0

this part of code replace information in file, how to save information and everytime update It? I mean I want have like a history of answers in text file.

try {
    File file = new File("src/test/History.txt");

    if (!file.exists()) {
        file.createNewFile();
    }

    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("bla bla bla");
    bw.close();


} catch (IOException e) {
    e.printStackTrace();
}
Kai
  • 38,985
  • 14
  • 88
  • 103

2 Answers2

4

If you want to append to a file that you have previously written to, use:

FileWriter(String fileName, boolean append)
mxns
  • 199
  • 1
  • 7
  • Yes, I want append. And how in my code must look this line? I need to delete my `FileWriter fw = new FileWriter(file.getAbsoluteFile());`? –  Nov 13 '12 at 12:35
  • No, just add the argument `true` to the FileWriter constructor – mxns Nov 13 '12 at 12:48
1

You want to open your file in append mode. To do so, you must add a boolean in the FileWriter constructor.

FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);

See the FileWriter documentation for more infirmations.

Magus
  • 14,796
  • 3
  • 36
  • 51