想問用java 寫野入 txt

如我第一次run佢就黎寫左 Now is the time  For all good student To say Thank You TO their teachers ,我想第二次run會write 果句在之前果句之下,唔係取代之前果句,個program應該點改

import java.io.*;
public class write {
        public static void main (String[]args){
String fileName ="file1.txt";
try{
FileWriter f =new  FileWriter (fileName);

f.write("Now is the time \n");
f.write("For all good student\n");
f.write("To say Thank You\n");
f.write("TO their teachers \n");
f.close();
}
catch(IOException e){
e.printStackTrace();
}

}

}

TOP

仲有點先可以write 果時 new line

TOP

回復 3# kstse2002


試下用一個PrintWriter encapsulate你個FileWriter

TOP

本帖最後由 kstse2002 於 2010-11-13 10:01 編輯

如果我想第一次run呢個program寫個句就加個1係前面,第2次就加個2,咁點先做到,有無得儲住個數字,下次已知已到1,x++左佢

TOP

FileWriter(File file, boolean append)
FileWriter f =new  FileWriter (fileName);
change constructor to FileWriter (fileName,true);

TOP