Android - Persist file when app closes.

Posted by Donal Rafferty on Stack Overflow See other posts from Stack Overflow or by Donal Rafferty
Published on 2010-06-14T16:13:46Z Indexed on 2010/06/15 8:42 UTC
Read the original article Hit count: 334

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 but the problem still remains.

© Stack Overflow or respective owner

Related posts about java

Related posts about android