How to append text to an existing file in Java?

You can use fileWriter with a true for appending.

try

{

    String filename= "MyFile.txt";

    FileWriter fw = new FileWriter(filename,true); //the true will append the new data

    fw.write("add a line

");//appends the string to the file

    fw.close();

}

catch(IOException ioe)

{

    System.err.println("IOException: " + ioe.getMessage());

}