Java FileWriter with append mode

Pass true as a second argument to FileWriter to turn on "append" mode.

fout = new FileWriter("filename.txt", true);

FileWriter usage reference


From the Javadoc, you can use the constructor to specify whether you want to append or not.

public FileWriter(File file, boolean append) throws IOException

Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.


You can open the FileWriter in append mode by passing true as the second parameter:

fout = new FileWriter("Distribution_" + ... + ".txt", true);

You may pass true as second parameter to the constructor of FileWriter to instruct the writer to append the data instead of rewriting the file.

For example,

fout = new FileWriter( "Distribution_" + Double.toString(lowerBound) + "" + Double.toString(_highBound) + ".txt",true);

Hope this would solve your problem.