Tuesday, May 10, 2011

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());
}
}

No comments:

Post a Comment

Chitika