Can apache FileUtils.writeLines() be made to append to a file if it exists

Ben picture Ben · Oct 27, 2009 · Viewed 18.6k times · Source

The commons FileUtils look pretty cool, and I can't believe that they cannot be made to append to a file.

File file = new File(path);
FileUtils.writeLines(file, printStats(new DateTime(), headerRequired));

The above just replaces the contents of the file each time, and I would just like to keep tagging this stuff to end just as this code does.

fw = new FileWriter(file, true);
try{
    for(String line : printStats(new DateTime(), headerRequired)){
        fw.write(line + "\n");
    }
}
finally{ 
    fw.close();
}

I've searched the javadoc but found nothing! What am I missing?

Answer

David Rabinowitz picture David Rabinowitz · Oct 27, 2009

You can use IOUtils.writeLines(), it receives a Writer object which you can initialize like in your second example.