I have made a resource reader with some example code from here. Here is my code…
public static void ReadFile() {
BufferedReader reader = new BufferedReader(new InputStreamReader(file));
String line;
try {
while((line = reader.readLine()) != null){
String[] values = line.split(",");
Log.d(TAG, String.valueOf(values.toString()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
my file is read onResume using this code…
InputStream file = getResources().openRawResource(R.raw.csvfile);
and I think i’ve got it right but when I use the
Log.d()
utility to read the values in my csv file (as shown above) I get something else instead
10-21 20:38:44.096: DEBUG/input(15223): [Ljava.lang.String;@467bbb20
10-21 20:38:44.104: DEBUG/input(15223): [Ljava.lang.String;@467bbfd8
10-21 20:38:44.104: DEBUG/input(15223): [Ljava.lang.String;@467bc3e0
10-21 20:38:44.112: DEBUG/input(15223): [Ljava.lang.String;@467bc7e8
10-21 20:38:44.112: DEBUG/input(15223): [Ljava.lang.String;@467bcbf0
10-21 20:38:44.112: DEBUG/input(15223): [Ljava.lang.String;@467bcff8
10-21 20:38:44.112: DEBUG/input(15223): [Ljava.lang.String;@467bd400
10-21 20:38:44.120: DEBUG/input(15223): [Ljava.lang.String;@467bd808
10-21 20:38:44.120: DEBUG/input(15223): [Ljava.lang.String;@467bdc10
10-21 20:38:44.120: DEBUG/input(15223): [Ljava.lang.String;@467bde60
now I don’t know if you can return straight from a BufferReader like this but there seems to be considerably less values here than there are in my csv file. Here is my csv file
001, 000, 000, 000, 000, 000,
001, 000, 000, 000, 000, 000,
001, 000, 000, 000, 000, 000,
001, 000, 000, 000, 000, 000,
001, 000, 000, 000, 000, 000,
001, 000, 000, 000, 000, 000,
001, 000, 000, 000, 000, 000,
001, 000, 000, 000, 000, 000,
001, 000, 000, 000, 000, 000
pretty empty i know but am I getting this right or should my log tags return some readable data, if so what am I doing wrong here.
values.toString()is only going to output a representation of the values array (its location in memory, in this case). You need to output the individual values. Something like this:EDIT: You’re also reinitializing
valueson every run of the while loop. You could either use a StringBuilder, and just keep appending the lines till you reach the end, and then split it into an array, or you could use an ArrayList, store each line in a local String, split it, and append those values to the outer ArrayList.No problem. 🙂 Just for future readers, I’ll be glad to add an example anyway.
With a StringBuilder:
Or appending per line:
These are off my head, so don’t fault me if they don’t compile as-is, haha.