I wanted to read and write specific lines from a CSV file.
Here is an example of my CSV
ID; Code; Name
1; ABHD; Paul
2; HYDR; Arthur
3; POAJ; Jake
4; PLMH; Georges
Actually I know how to read all the file and put it into a list of lines by myFile.readLines().
But I want to read a specific line like the line with the Code ABHD, and later on modify that specific Line without reading and writing the whole file. Any idea ?
Thanks by advance.
A CSV file is a not a database. You can't write individual elements inside the file. You need to read the entire file and write the entire file. If the file is a reasonable size, you can read the entire file into memory, alter the data you want to change (in memory) and then write the entire file again. If the file is huge, you probably need to make your modifications "on the fly" (ie: read a line, (optionally) change it, write the line to the output file).
There are libraries that you can use to parse the data, as CSV can be complicated to parse (especially if it contains text strings).
See https://www.baeldung.com/kotlin/csv-files for some help or search for "kotlin csv parse"
Related
i can read the next line in a file stored in the assets folder.
But how do read the previous line by click of a button.
my file is stored in the assets folder
if you got a small size file, just read all previous lines starting from the first line to the line you can read or even to the last line , split them by "\n" and get the previous line by its index, this is not a good way, however i recommend you to post your code so we can help you better ^_*
I am really lost here, i am trying to find my way around xml parsing, reading and writing.
I have this app where at one point i can input data such as a Date and a time for instance - click save, and once it saved it will write into an existing XML file, for later reading, and add it at the end in a format like this:
<Units>
<item>
<date>27-5-12</date>
<time>15:30</time>
</item>
<item>
... and so on ...
</Units>
i managed to read an xml file, but i am really having trouble in opening a premade - existing file for reading or writing.
currently i tried this code:
InputStream raw = this.getAssets().open("mydata.xml");
Reader is = new BufferedReader(new InputStreamReader(raw, "UTF8"));
which returns file not found exception.
could anyone direct me on what i should look for?
Thanks.
As written, your source XML file is located in your APK's assets directory - everything in your APK is read-only, so you won't be able to write to that file. (Also, you should probably put that XML data into the res/xml directory instead of the assets directory, unless you have a compelling reason to do otherwise.)
If the XML file isn't very long/complex, you could read the assets file into a structure, then add your new data to that structure, and write a new XML file into your app's data directory with the updated data. This approach has the advantage that you can have multiple source files feeding into one main file per app.
A more flexible and open-ended option would be to set up a database table. When the app is first installed, you load/update the table with data from the assets file. As your app keeps adding timestamped data, you just add new rows to the table. This approach also has the advantage that you can easily update the source data or the database structure with each app update - it's harder to compare old vs new data if it's stored internally in XML format.
I didn't see the assets folder in my project, i placed my xml file there and it works now :)
Pretty short question:
I have put a .csv file in my assets folder. My problem is that I'm unsure how to read it and store its contents in an array-like form that I can pass to a function that then loops through the contents.
So, basically I want something like (pseudo code):
VariableArray var[] = new Variable (get the file contents);
performFunction(var[]);
How I can accomplish this?
You can use this to get an InputStream to your file in assets.
InputStream is = getAssets().open(fileName);
Then you just need to make a reader and loop through the file until the end of it.
I suggest you to use the JavaCSV library to deal with CSV formats. There are a lot of things it can accomplish in very few lines of code.
Hey, I have a lot of Strings that I use into my app, the .txt file that I use has ~14000 lines.. and each 3-10 lines are divided into sections like <String="Chapter I"> ... </String> ..
Speaking of performance/speed, should I put the sections into a Database, Or read line by line through the .txt file and check if the section number is the current one? Will this affect speed/performance?
I could also divide each ~2000 lines into a different .txt file so there would be less lines to go through. Is this a bad way of storing data? Thanks
I think sqlite would do the trick. It will probably be way faster than parsing a text file, plus you wont have to maintain the headache of your own ad hoc text database, or build a parser in the first place. Basically, use it, its way easier.
The standard way to deal with Strings in Android is to put them into res/values/strings.xml (I'm pretty sure you can have multiple String files in that directory if you like). If you are developing in Eclipse it will automatically populate the R class (the resource class) with constants that you can use to reference these Strings in your code:
R.string.mystring
Or in XML layouts:
#string/mystring
Or if you're doing something more custom you can use:
String string = getString(R.string.hello);
I would definitely choose this over a .txt file. It's much easier. All the work is done for you! Have a read of this Android article about it.
This is what a database is for. Use it.
I have some reference data in a text file (~5MB) that I want to use with might android application.
The file is of the format:
1|a|This is line 1a
1|b|This is line 1b
2|a|This is line 2a
2|b|This is line 2b
2|c|This is line 2c
What I want to know is the most efficient way (less memory, fast, size etc.) to use this file within my application.
a.) Should I save the file as a raw resource and open and read the whole file whenever I need a certain line.
b.) Should I convert the file to XML and use XPath to query the file when ever I need to look up a value
<!--sample XML -->
<data>
<line number="1">
<entry name="a">This is line 1 a</entry>
</line>
</data>
c.) Should I just copy & paste the whole file as a static string array in the application and use that.
... any other suggestions are welcome.
[EDIT]
I will also need to search this file and jump to arbitrary keywords e.g. "line 1a".
XML will always take longer to read than simple text or CSV files. What XML gives you in the tradeoff is a highly structured and reliable way of storing and retrieving data. XML files are, as you can see in the examples above, a good 2-3x larger than the data they actually contain.
If you're sure that you're never going to run into the "delimiter" character in your simple text file, then that would probably work just fine, purely from a file speed perspective.
You have not provided enough information to answer this question. However, if I were a betting man, the answer is probably "none of the above".
I will also need to search this file
What does this mean? You are searching by some string key? By some regular expression? By a SQL-style query string where certain portions of a line are interpreted as integers versus strings versus something else? By a Google search-style string?
Each of those answers probably dictates a different technology for storing this information.
I will also need to...jump to arbitrary lines.
Why? How are you determining which "arbitrary lines" you are "jump"ing to: key? line number? byte offset? search results? something else?
And, of course, there are other questions, like:
How often is this data updated?
How is this data updated: new version of the app? download the whole file? download deltas/diffs? something else?
Is the data ASCII? UTF-8? Something else?
and so on.
Something that size that must be searched upon suggests "use a SQLite database", but some of the other answers might steer away from that solution.
If you are talking about very small amounts of data, the Android XML compiler can produce very efficient binary representations for you that you can access just like XML. On the other hand if the data is very large at all, and you need arbitrary queries, I would expect SQLlite to win out on performance (as well as flexibility). A small benchmark should be easy to write and would give you a good idea as to the basic tradeoffs involved.
Flat-files would be a last option, imo, but could work if the file isn't very large.
If you define efficiency as (less memory, fast, size etc.), a flat or delimited file will be faster to load and save.
However, people use XML because they are willing to trade some of that speed for XML's greater flexibility and ease of use.