Java FileWriter有什么问题吗?

Java FileWriter有什么问题吗?,java,io,Java,Io,我不知道FileWriter是怎么回事,因为它只写出HTML部分,而没有从字符串数组内容中写出任何内容内容存储大量长字符串s。是因为Java的垃圾收集器吗 我打印出内容,所有内容都在那里,但是FileWrter除了HTML部分之外,没有将内容写入该文件。我添加了System.out.println(k)在增强的for循环中内容数组不为空 public void writeHtml(String[] content) { File file = new File("final.html")

我不知道
FileWriter
是怎么回事,因为它只写出HTML部分,而没有从
字符串
数组
内容中写出任何内容<代码>内容
存储大量长
字符串
s。是因为Java的垃圾收集器吗

我打印出
内容
,所有内容都在那里,但是
FileWrter
除了HTML部分之外,没有将
内容
写入该文件。我添加了
System.out.println(k)在增强的for循环中<代码>内容
数组不为空

public void writeHtml(String[] content) {
    File file = new File("final.html");
    try {
        try (FileWriter Fw = new FileWriter(file)) {
            Fw.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
                    + "<html>\n"
                    + "<head>\n"
                    + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=us-ascii\">\n"
                    + "<title>" + fileName +" for  The Delinquent</title>\n"
                    + "<style type = \"text/css\">\n"
                    + "body {font-family: \"Times New Roman, serif\"; font-size: 14 or 18; text-align: justify;};\n"
                    + "p { margin-left: 1%; margin-right: 1%; }\n"
                    + "</style>\n"
                    + "</head><body>");
            for (String k : content) {
                Fw.write(k+"\n");
            }
            Fw.write("</body></html>");
        }

    } catch (Exception e) {
        e.printStackTrack();
    }
}

一切都打印出来了。太奇怪了:(

你的代码正在工作。唯一阻止
内容
被写入的东西是空的
内容
。它没有元素。

你的代码基本正确,可能内容数组是空的。 以下是现代化的java风格

public void writeHtml(String[] content) {
    Path file = Paths.get("final.html");
    try (BufferedWriter fw = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
        fw.write("<!DOCTYPE html>\n"
                + "<html>\n"
                + "<head>\n"
                + "<meta charset=UTF-8\">\n"
                + "<title>" + fileName + " for  The Delinquent</title>\n"
                + "<style type = \"text/css\">\n"
                + "body {font-family: \"Times New Roman, serif\";"
                + " font-size: 14 or 18; text-align: justify;};\n"
                + "p { margin-left: 1%; margin-right: 1%; }\n"
                + "</style>\n"
                + "</head><body>");
        fw.write("Content has: " + content.length + " strings.<br>\n");
        for (String k : content) {
            fw.write("* " + k + "<br>\n");
        }
        fw.write("</body></html>\n");
    } catch (IOException e) {
        System.out.println("Error " + e.getMessage());
    }
}
public void writeHtml(字符串[]内容){
Path file=Path.get(“final.html”);
try(BufferedWriter fw=Files.newBufferedWriter(file,StandardCharsets.UTF_8)){
fw.写入(“\n”
+“\n”
+“\n”

+“除HTML部分外”是什么意思?能否向我们展示当前文件内容的外观?对于新行,您需要使用“\n”“。请提供一个。您应该始终假设您的代码有问题,而不是全世界数百万开发人员使用的代码。是的,
FileWriter
有可能存在缺陷,但问题更可能存在于您的代码中。如果没有错误,我们将无法帮助您发现问题。”。“FileWriter有什么问题吗?"不。你可能会使用它,但为什么要假设它是
FileWriter
,而不是你自己的代码呢?FileWriter与你对其工作原理的假设相比,本质上有问题的几率是1到100000000。先看看你自己的假设。我更新了文章,内容不是空的,因为我试着打印它t、 我更新了帖子,内容不是空的,因为我试着把它打印出来。而且输出的确实是纯ASCII、拉丁字母等等?也试试我的代码,以便比较。
 for (String k: content) {
                System.out.println(k + "\n");
                bw.write(k + "\n");
            }
public void writeHtml(String[] content) {
    Path file = Paths.get("final.html");
    try (BufferedWriter fw = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
        fw.write("<!DOCTYPE html>\n"
                + "<html>\n"
                + "<head>\n"
                + "<meta charset=UTF-8\">\n"
                + "<title>" + fileName + " for  The Delinquent</title>\n"
                + "<style type = \"text/css\">\n"
                + "body {font-family: \"Times New Roman, serif\";"
                + " font-size: 14 or 18; text-align: justify;};\n"
                + "p { margin-left: 1%; margin-right: 1%; }\n"
                + "</style>\n"
                + "</head><body>");
        fw.write("Content has: " + content.length + " strings.<br>\n");
        for (String k : content) {
            fw.write("* " + k + "<br>\n");
        }
        fw.write("</body></html>\n");
    } catch (IOException e) {
        System.out.println("Error " + e.getMessage());
    }
}