Append to file in java

See this article to see about FileWriter and BufferedWriter in java. We have already seen how to write to file in java. Appending is on the same lines, just adding true to FileWriter object.

FileWriter fstream = new FileWriter("out.txt",true);

Full example:

public static void appendToFile(String fileName)
{
try{
//Create file
FileWriter fstream = new FileWriter(fileName,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello I just wanted to append");
//Close the output stream
out.close();
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}

About kinshuk4
I am computer science and engg student and rightly working in good investment bank.

Leave a comment