繁体   English   中英

PrintWriter创建文件但不写入

[英]PrintWriter creates file but doesn't write

我在某个地方的网站上使用了示例代码,它看起来像这样:

package gdt.enlightening;

import notify.*;
import javax.swing.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class export {
    public static void Export(String path) {

        try {
            // Package.json
            File file = new File(path + "/package.json");

            FileWriter pw = new FileWriter(file);
            pw.write("test");
            pw.write("Hi!");

            pw.write("    \"id\": \"" + main.packageID + "\",\r\n");
            pw.write("    \"name\": \"test\",");

            notify.Notify.info("GDT Enlightening", "Finished exporting without errors.");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

它创建文件,但将其完全留空。 我似乎不知道为什么。 我需要一个“文件”对象吗?

我已经尝试过在这里找到不同的解决方案,但是它不起作用。 我也尝试过打印方法。

编辑:通过在末尾调用pw.close()进行修复

您应该添加pw.close()来解决此问题。

否则,数据将丢失在缓冲区中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM