views:

162

answers:

1

I am creating a file in my Android application as follows:


HEADINGSTRING = new String("Android Debugging " + "\n"
                    "XML test Debugging");

}

public void setUpLogging(Context context){

    Log.d("LOGGING", "Setting up logging.....");
    try { // catches IOException below

         FileOutputStream fOut = context.openFileOutput(FILE_NAME,Context.MODE_APPEND);

         OutputStreamWriter osw = new OutputStreamWriter(fOut); 

         // Write the string to the file

         osw.write(HEADINGSTRING);

        /* ensure that everything is

        * really written out and close */

        osw.flush();

       osw.close();

} catch (FileNotFoundException e) {

    e.printStackTrace();
} catch (IOException e) {

    e.printStackTrace();
}
    finally{
        Log.d("LOGGING", "Finished logging setup.....");
    }
}

And I write to the file during the running of the app as follows:


public void  addToLog(File file, String text) throws IOException {

         BufferedWriter bw = new BufferedWriter (new FileWriter(file, true));

         bw.write ("\n" + text);

         bw.newLine();

         bw.flush();
         bw.close();
}

This works fine but when my app closes the file gets deleted and when the app is run again all the information I wrote to it is gone.

How can I make sure the file persists even after closure of the app?

Update:

I have changed MODE_PRIVATE to MODE_APPEND and the problem is fixed, thanks Skirmish.

+1  A: 

You might want to check http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int) especially the part about MODE_APPEND rather than clearing everything out every time the app starts.

skirmish
That worked but when I try to use Mode_Private again so as to not append to the file it doesn't work!
Donal Rafferty