<小时/>
您好,关闭 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");
最佳答案
流的生命周期是:
- 创建(
新流
) - 处理(
写入
、追加
等) - 关闭(
关闭
)
来自 PrintWriter::close()
文档:
Closes the stream and releases any system resources associated with it.
PrintWriter::close()在 PrintWriter
内设置 out = null
。
关闭后,您将在任何调用 write
、flush
、print
等时得到 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/