【问题标题】:Read/Write to a file does not write to the output读取/写入文件不写入输出
【发布时间】:2023-04-09 11:00:01
【问题描述】:

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

输入文件:

Christopher kardaras,10 N Brainard,Naperville,IL,60566 George 华盛顿,30 W Jackson,芝加哥,IL,60060

输出文件:

Christopher kardaras 10 N Brainard Naperville, IL 60566

乔治·华盛顿 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();

【问题讨论】:

    标签: java file-io outputstream printwriter


    【解决方案1】:

    在关闭outFile之前尝试flushing

    outFile.flush();
    

    您也可以使用 alternative PrintWriter constructor 为您处理这些问题:

    public PrintWriter(OutputStream out, boolean autoFlush)
    

    【讨论】:

    • 写入时...先刷新内容,最后关闭! :)
    猜你喜欢
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多