0

The question is how to append to an existing file. I'm using MODE_APPEND with FileOutputStream fos = openFileOutput("filename", MODE_APPEND);. The file is created and the object is saved, but when I call the method again on the same file - nothing happens. MODE_PRIVATE works but only for 1 insert as it creates a new file every time it's called. I spent the whole day researching and I couldn't find any answer so I am desperate for your help.

public void createFile (View V) throws IOException, JSONException {
    JSONArray resultsLog = new JSONArray();
    JSONObject exercise2;

    String pickVal1 = stringArray[numPick1.getValue()];
    String pickVal2 = stringArray[numPick2.getValue()];
    String pickVal3 = stringArray[numPick3.getValue()];
    String pickVal4 = stringArray[numPick4.getValue()];

    exercise2 =new JSONObject();
    exercise2.put("rep", pickVal1 + " kg  " + pickVal2 + " kg  " + pickVal3 + " kg  " + pickVal4 + " kg  ");
    exercise2.put("type", caller + "  on  " + date);
    resultsLog.put(exercise2);

    String text= resultsLog.toString();

    FileOutputStream fos = openFileOutput("resultsDat3", MODE_APPEND);

    fos.write(text.getBytes());
    fos.close();

    //displayText(this,R.id.stext,resultsLog.toString());

    // finish();
}
Saurabh
  • 434
  • 1
  • 4
  • 12
Android
  • 814
  • 1
  • 9
  • 21
  • Did you try [`FileWriter`](http://stackoverflow.com/questions/5678917/how-to-append-to-a-text-file-in-android) – Uma Kanth Jun 04 '15 at 16:38

1 Answers1

0

Use a FilterWriter object. It provides a constructor with the option to append writes to the current file, and even takes care of creating the file if needed.

Submersed
  • 8,810
  • 2
  • 30
  • 38