gpt4 book ai didi

java - 读/写文件不会写入输出

转载 作者:行者123 更新时间:2023-12-01 15:05:39 29 4
gpt4 key购买 nike

我正在编写一个程序,该程序从一个文件读取输入,然后该程序将格式化数据并将其写入另一个文件。

输入文件:

Christopher kardaras,10 N Brainard,Naperville,IL,60566 George Washington,30 W Jackson,Chicago,IL,60060

输出文件:

Christopher kardaras 10 N Brainard Naperville, IL 60566

George Washington 30 W Jackson Chicago, IL 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();

最佳答案

尝试flushing关闭之前的 outFile。

outFile.flush();

您还可以使用alternative PrintWriter constructor它会为您解决这个问题:

public PrintWriter(OutputStream out, boolean autoFlush)

关于java - 读/写文件不会写入输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12991136/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com