简体   繁体   中英

Text or String written on text file overlaps the previous word. It should write each word line by line

I am able to access the file I created using this code that saves into the file every word inputted from EditTExt in Android. However, after inputting more words the only the last word was written on the file. It seems that the previous words was overwritten after pressing the SAVE button. This button is clicked each time a word is to be saved in the text file. I wanted to list the word inputted line by line on the text file. What should be done to do this?

here's the code i used.

File dir = new File (getFilesDir(), "myFolder");
dir.mkdirs();
File file = new File(dir, "myData.txt");

try {
    FileOutputStream f = new FileOutputStream(file);
    PrintWriter pw = new PrintWriter(f);


    pw.println("\n" + stringWord);


    pw.flush();
    pw.close();
    f.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();

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

FileOutputStream以“覆盖”模式打开文件,以附加到文件使用:

FileOutputStream f = new FileOutputStream(file, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM