Is it possible to parse a text file with a single line divided in values in Android? An example of file i would like to parse could be:
100,102,106,109,110
And the solution I'm looking for would be something like
Log.d(TAG, "value 1" + theFirstValue);
Log.d(TAG, "value 2" + theSecondValue);
//etc...
Any suggestions? Thanks!
You could use the String.split() method.
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "path/to/the/file");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(",");
for(int i = 0; i < split.length; i++) {
Log.d(TAG, "value " + i + ": " + split[i]);
}
}
br.close();
String s="100,102,106,109,110";
String[] values=s.split(",");
for(int i=0;i<values.length;i++){
int v=Integer.parseInt(values[i]);
Log.d(TAG, "value "+i +": "+ v);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With