我在某个地方的网站上使用了示例代码,它看起来像这样:
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()
来解决此问题。
否则,数据将丢失在缓冲区中。