0

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

输入文件:

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

输出文件:

Christopher kardaras 10 N Brainard Naperville, IL 60566

乔治华盛顿 30 W 杰克逊 芝加哥,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();
4

1 回答 1

1

在关闭它之前尝试刷新outFile。

outFile.flush();

您还可以使用替代的 PrintWriter 构造函数来为您处理:

public PrintWriter(OutputStream out, boolean autoFlush)
于 2012-10-20T17:56:38.180 回答