对文件的读/写不会写入输出

时间:2012-10-20 17:55:40

标签: java file-io outputstream printwriter

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

输入文件:

  

Christopher kardaras,10 N Brainard,Naperville,IL,60566 George   华盛顿,30 W Jackson,芝加哥,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();

1 个答案:

答案 0 :(得分:1)

在关闭之前尝试flushing outFile。

outFile.flush();

您还可以使用alternative PrintWriter constructor来处理这件事:

public PrintWriter(OutputStream out, boolean autoFlush)