-1

I am trying to read all lines from a couple files and places those lines in ArrayLists. 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 ArrayLists (numberCorrectSet and numberDateSet are the String ArrayLists):

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.

bcsb1001
  • 2,606
  • 3
  • 22
  • 33

4 Answers4

0

I am not sure what you are trying to write. Per your code, only the content of reference 'date' will be written to the output file In order to write the content of array list. You need to use for each.

for (String text : numberDataSet) {
     wr.append (text);
}

Please explain little more if u felt my answer is not relevant.

And statements. wr.flush () and wr.close () should come under finally block

JP Bose
  • 22
  • 5
  • Thank you for responding, I'm trying to write the date and the score to 2 separate files. Those files are "dates_file" and "scores_file". – John Dough Jun 16 '16 at 17:24
  • Try to print the content of array list after the read process done using sysout (). This will help us to make sure whether all lines of file read by the system – JP Bose Jun 16 '16 at 17:33
  • Only one line was printed – John Dough Jun 16 '16 at 18:41
0

Try making these changes:

    FileReader file = new FileReader("c:/yourFile.txt");
    BufferedReader br = new BufferedReader(file);

    String line = br.readLine();
    while (line != null) {
        numberDateSet.add(line);
        line = br.readLine();
    }
    file.close();
henrique romao
  • 540
  • 8
  • 26
0

Try this Java 7 code:

new String(Files.readAllBytes(...)) 

or:

Files.readAllLines(...)
james.garriss
  • 11,468
  • 6
  • 75
  • 94
ceph3us
  • 6,591
  • 2
  • 34
  • 39
0

Try changing

Writer wr = new OutputStreamWriter(getApplicationContext().openFileOutput(datesFileName, MODE_PRIVATE));

to

Writer wr = new OutputStreamWriter(getApplicationContext().openFileOutput(datesFileName, MODE_APPEND));

and

wr.append(date);

to

wr.append(date + "\n");

for each of the files.

Edit: Explanation as requested. The first change is to make sure the Writer is appending to the file. The method name append is somewhat deceiving, it simply means that you are appending to what was written in this session. To append to the file, you must specify you want to when making the FileOutputStream. The second is to make sure each date goes on a new line, so rather than

datedatedate

write them as

date
date
date
yesennes
  • 763
  • 1
  • 6
  • 15