gpt4 book ai didi

java - 创建 html 文件并动态添加行

转载 作者:搜寻专家 更新时间:2023-11-01 03:48:39 24 4
gpt4 key购买 nike

我想使用 java 创建 log.html 文件,其中我会在捕获到错误时将错误转储到 html 表行中。我创建了表列,但是当我向它传递任何内容时,我的数据会覆盖列名而不是附加行。我的java代码如下:

public static void main(String[] args) throws IOException
{
String month = getMonth(1);
System.out.println(month);
//table(month);
addrow("abc");
}
public static void table(String Update) throws IOException
{
//define a HTML String Builder
StringBuilder htmlStringBuilder=new StringBuilder();
htmlStringBuilder.append("<html><head><title>Packaging Issues</title></head>");
htmlStringBuilder.append("<body><h1> Files Received </h1>");
htmlStringBuilder.append("<table border=\"1\" bordercolor=\"#000000\">");
htmlStringBuilder.append("<tr><td><b>Cycle</b></td><td>"+Update+"</td></tr>");
htmlStringBuilder.append("<tr><td><b>Version</b></td><td></tr>");
newtable(htmlStringBuilder.toString());
}
public static void newtable(String a)
{
StringBuilder htmlStringBuilder=new StringBuilder();
htmlStringBuilder.append(a);
htmlStringBuilder.append("<h1> Issues Found </h1>");
htmlStringBuilder.append("<table border=\"1\" bordercolor=\"#000000\">");
htmlStringBuilder.append("<tr><td><b>Type</b></td>");
htmlStringBuilder.append("<td><b>Field</b></td>");
htmlStringBuilder.append("<td><b>Issue</b></td></tr>");
addrow(htmlStringBuilder.toString());
// WriteToFile(htmlStringBuilder.toString(),"log.html");
}

public static void addrow(String a)
{
try {
StringBuilder htmlStringBuilder=new StringBuilder();
htmlStringBuilder.append("<tr><td><b>"+a+"</b></td>");
htmlStringBuilder.append("<td><b>Field</b></td>");
htmlStringBuilder.append("<td><b>Issue</b></td></tr>");
htmlStringBuilder.append("</table></body></html>");
WriteToFile(htmlStringBuilder.toString(),"log.html");
} catch (IOException e) {
e.printStackTrace();
}
}

这是 writetofile 方法:

  public static void WriteToFile(String fileContent, String fileName) throws IOException {
String projectPath = "/home/abc";
String tempFile = projectPath + File.separator+fileName;
File file = new File(tempFile);
// if file does exists, then delete and create a new file
//write to file with OutputStreamWriter
OutputStream outputStream = new FileOutputStream(file.getAbsoluteFile());
Writer writer=new OutputStreamWriter(outputStream);
writer.write(fileContent);
writer.close();

}

最佳答案

构造函数FileOutputStream(file) 覆盖文件的内容。使用 this constructor相反

FileOutputStream fos =new FileOutputStream(file, true) ;

关于java - 创建 html 文件并动态添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35124308/

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