I am trying to read all lines from a couple files and places those lines in ArrayList
s. Despite having several lines, only one line from each file is being read and added to an ArrayList
.
Here's the code for writing to the 2 files:
String date = month + "/" + dayOfMonth + "/" + year;
String datesFileName = "dates_file";
String scoresFileName = "scores_file";
//this next part, commented out or not, doesn't make a difference in the results
/*File datesFile=new File(getApplicationContext().getFilesDir(), datesFileName);
if(!datesFile.exists()){
try{
datesFile.createNewFile();
datesFile.mkdir();
Log.i("ARGH", "created a new file");
}catch (Exception e){
e.printStackTrace();
}
}
File scoresFile=new File(getApplicationContext().getFilesDir(), scoresFileName);
if(!scoresFile.exists()){
try{
scoresFile.createNewFile();
scoresFile.mkdir();
Log.i("ARGH", "created a new file2");
}catch(Exception e){
e.printStackTrace();
}
}*/
//Here is where the writing takes place
try {
Writer wr = new OutputStreamWriter(getApplicationContext().openFileOutput(datesFileName, MODE_PRIVATE));
wr.append(date);
wr.flush();
wr.close();
Writer wr2 = new OutputStreamWriter(getApplicationContext().openFileOutput(scoresFileName, MODE_PRIVATE));
wr2.append(correctlyAnswered + "");//correctlyAnswered is an int
wr2.flush();
wr2.close();
} catch (Exception e) {
e.printStackTrace();
}
Here's the code for reading from the 2 files and placing the line(s) into ArrayList
s (numberCorrectSet
and numberDateSet
are the String
ArrayList
s):
numberOfQuestions = 5;
String datesFileName = "dates_file";
String scoresFileName = "scores_file";
try {
//reading the dates
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput(datesFileName)));
while ((line = br.readLine()) != null) {
numberDateSet.add(line);
}
br.close();
Log.i("ARGH", numberDateSet.size() + " dates");
//reading the questions answered correctly number
BufferedReader br2 = new BufferedReader(new InputStreamReader(openFileInput(scoresFileName)));
while ((line = br2.readLine()) != null) {
numberCorrectSet.add(line);
}
br2.close();
Log.i("ARGH", numberCorrectSet.size() + " scores");
} catch (Exception e) {
e.printStackTrace();
}
I googled and searched Stack Overflow for a similar problem, but found no similar situation.