【问题标题】:printWriter does not print to file [duplicate]printWriter 不打印到文件 [重复]
【发布时间】:2017-03-11 21:53:26
【问题描述】:

我运行了这段代码,但我发现输出文件是空的。

public static void main(String[] args) {
    try {
        File outputFile=new File("out");
        PrintWriter output= new PrintWriter(outputFile);
        output.println("test");
    } catch (FileNotFoundException ex) {
        System.out.println(ex.getMessage());
    }

}

【问题讨论】:

  • 您没有关闭 PrintWriter。使用 try-with-resources。

标签: java printwriter


【解决方案1】:

你应该永远记住close流。

只要加output.close(),问题就解决了。

当然,你最好把它加到finally块下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    相关资源
    最近更新 更多