gpt4 book ai didi

java - 关闭 PrintWriter 后无法写入新的 HTML 内容

转载 作者:行者123 更新时间:2023-12-02 03:34:56 25 4
gpt4 key购买 nike

<小时/>

您好,关闭 PrintWriter 后无法添加新内容。还有其他方法可以做到这一点吗?

String myCurrentDir = System.getProperty("user.dir");
File htmlFile = new File(TestRunner.FilePath+"\\Footer.html");
final OutputStream out = new BufferedOutputStream(new FileOutputStream(htmlFile));
PrintWriter writer = new PrintWriter(out);
writer.println("<HTML>");
writer.println("<HEAD>");
writer.println("<title>Report Foot</title>");
writer.println("<link rel=\"stylesheet\" type=\"text/css\" href=\""+myCurrentDir+"/CSS/Report.CSS\">");
writer.println("</HEAD>");
writer.println("<body class = 'footer'>");
writer.println("<B>");
writer.println("Total : "+TotalTCs);
writer.println("<span class='passed'>Passed : "+0+"</span>");
writer.println("<span class='failed'>Failed : "+0+"</span>");
writer.println("<span class='warned'>Warned : "+0+"</span>");
writer.println("<span class='norun'>No Run : "+0+"</span>");
writer.println("</B>");
writer.println("<BR/>");
writer.println("<BR/>");
writer.close();//after this any content doesn't gets saved in html
writer.println("@ABC DEF XYZ");
writer.println("</body>");
writer.println("</HTML>");
writer.close();
System.out.println("Footer Written");

最佳答案

流的生命周期是:

  1. 创建(新流)
  2. 处理(写入追加等)
  3. 关闭(关闭)

来自 PrintWriter::close() 文档:

Closes the stream and releases any system resources associated with it.

PrintWriter::close()PrintWriter 内设置 out = null。关闭后,您将在任何调用 writeflushprint 等时得到 IOException。这些方法在执行任何操作之前都会调用 ensureOpen()

private void ensureOpen() throws IOException {
if (out == null)
throw new IOException("Stream closed");
}
}

关于java - 关闭 PrintWriter 后无法写入新的 HTML 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com