Java FileWriter with append mode

Progress Programmer picture Progress Programmer · Aug 4, 2009 · Viewed 84.1k times · Source

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");
    fileout.close();

Answer

Amber picture Amber · Aug 4, 2009

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

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

FileWriter usage reference