小编use*_*017的帖子

PrintWriter创建文件但不写入

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

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它创建文件但完全为空.我似乎没弄明白为什么.我需要一个"文件"对象吗?

我在这里尝试了不同的解决方案,但它不起作用.我也玩过打印方法.

编辑:通过调用固定pw.close()在年底

java printwriter

2
推荐指数
1
解决办法
4821
查看次数

标签 统计

java ×1

printwriter ×1