/* 使用PrintWriter可以更好地格式化要追加的文本内容
*/
import java.io.*;
public class Exercise {
public static void main(String args[]) {
try{
File file = new File("/home/zjz/Desktop/myFile1.txt");
if (!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
// This will add a new line to the file content
pw.println("");
/* Below three statements would add three mentioned Strings to rhe
file in new lines.
*/
pw.println("This is the first line.");
pw.println("This is the second line.");
pw.println("This is third line.");
pw.close();
System.out.println("Data successfully appended at the end of file");
} catch (IOException e) {
System.out.println("Exception occurred: ");
e.printStackTrace();
}
}
}
from: http://beginnersbook.com/2014/01/how-to-append-to-a-file-in-java/