以下實例示範了使用filewriter 方法向檔案中追加資料:
/*
author by w3cschool.cc
Main.java
*/import java.io.*;public class Main {
public static void main(String[] args) throws Exception {
try {
BufferedWriter out = new BufferedWriter
(new FileWriter("filename"));
out.write("aString1\n");
out.close();
out = new BufferedWriter(new FileWriter
("filename",true));
out.write("aString2");
out.close();
BufferedReader in = new BufferedReader
(new FileReader("filename"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
}
in.close();
catch (IOException e) {
System.out.println("exception occoured"+ e);
}
}}
以上程式碼執行輸出結果為:
aString1 aString2
以上就是Java 實例- 向檔案中追加資料的內容,更多相關請注意PHP中文網(www.php.cn)!