[英]Using PrintWriter to print a string to a text file, but when I open the text file it is empty
我使用Java中的PrintWriter嘗試將一些字符串寫入文本文件,但是當我打開文本文件時,它為空。
這是我的代碼:
public void save(String fileName) throws FileNotFoundException{
PrintWriter out1 = new PrintWriter("aSB.txt");
out1.println("The Pupil's information is listed below:");
for (Pupil pup : Pupils) {
out1.println(pup.getFirstName() + " " + pup.getLastName());
}
out1.println("End");
out1.println("The Club's information is listed below:");
for (Club clu : Clubs) {
out1.println(clu.getName());
}
out1.close();
}
為什么文件名是空的?
嘗試捕獲FileNotFoundException並查看是否在創建文件時出錯。 也可以嘗試捕獲SecurityException。 如果不是,請嘗試使用FileOutputStream。 使用起來真的很容易祝你好運!
正如Kevin所說,也許是目錄問題。 嘗試指定完整路徑,看看會發生什么。
就像是:
PrintWriter pw = new PrintWriter("c:/asb.txt");
另外,請看這里
很有可能數組是空的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.