2010-06-14 53 views
3

我创造了我的Android应用程序的文件,如下所示:Android的 - 坚持文件时,应用程序关闭


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....."); 
    } 
} 

我的应用程序的运行过程中写入文件,如下所示:


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(); 
} 

这工作正常,但当我的应用程序关闭时,文件被删除,当应用程序再次运行时,我写入的所有信息都消失了。

即使在关闭应用程序后,如何确保该文件仍然存在?

更新:

我已经改变MODE_PRIVATE到MODE_APPEND,问题是固定的,谢谢遭遇战。

回答