Reputation: 1112
I have a text file on my sdcard filled like this :
test1
test2
test3
I would like to populate a String array by reading the file and get an array like this :
String[] val = {"test0","test1","test2"};
This is how I read the file but how can I read each line and fill the array ? :
public void ReadSettings(){
try{
File f = new File(Environment.getExternalStorageDirectory()+"/file.txt");
FileInputStream fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
while ((readString = buf.readLine()) != null) {
Log.d("line: ", readString);
for (int i=0 ; i < 3 ; i++){
val[i] = readString;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
This is what I get unfortunetly :
String[] val = {"test2","test2","test2"};
Any help would be appreciated. Thanks !
Upvotes: 0
Views: 1381
Reputation: 63955
int i = 0;
while ((readString = buf.readLine()) != null) {
Log.d("line: ", readString);
val[i++] = readString;
// u better use an ArrayList or you have to check if i < val.size
}
Upvotes: 2