Java FileWriter with append mode


I'm currently using FileWriter to create and write to a file. Is there any way that I can write to the same file every time without deleting the contents in there?

   fout = new FileWriter(
    "Distribution_" + Double.toString(_lowerBound) + "_" + Double.toString(_highBound) + ".txt");
    fileout = new PrintWriter(fout,true);
fileout.print(now.getTime().toString() + ", " + weight + ","+ count +"\n");


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.

By : jatanp

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

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

This video can help you solving your question :)
By: admin