[英]Read/Write to a file does not write to the output
我正在編寫一個程序,該程序從一個文件讀取輸入,然后該程序將格式化數據並將其寫入另一個文件。
輸入文件:
克里斯托弗·卡達拉斯(Christopher kardaras),北布雷納德(N Brainard),內珀維爾(Naperville),伊利諾伊州60566,喬治華盛頓(George Washington),納克維爾(WJackson)30,芝加哥伊利諾伊州(60060)
輸出文件:
克里斯托弗·卡達拉斯(Christopher kardaras)10 N Brainard內珀維爾,IL 60566
喬治華盛頓30 W傑克遜芝加哥,伊利諾斯州60060
當我運行代碼時,輸出未顯示在輸出文件中,以下是我的代碼。
//open input, output files
FileReader freader = new FileReader("AddressData.txt");
BufferedReader inFile = new BufferedReader(freader);
FileWriter fwriter=new FileWriter("FormattedData.text");
PrintWriter outFile= new PrintWriter (fwriter);
//process data - get a line, separate into fields, then print
//address label to the output file
line= inFile.readLine();
while (line != null)
{
//Create a new scanner, use comma as field separator
Scanner s = new Scanner(line).useDelimiter(",");
// SOME CODE OMITTED HERE FOR BREVITY
out.printf(...);
//get the next line. read failure (EOF) will exit the loop
line = inFile.readLine();
}
//clean up
inFile.close();
outFile.close();
嘗試在關閉它之前刷新 outFile。
outFile.flush();
您還可以使用替代性的PrintWriter構造函數 ,該構造函數可以幫助您:
public PrintWriter(OutputStream out, boolean autoFlush)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.