为了账号安全,请及时绑定邮箱和手机立即绑定

通过在 java 中传递值将结果写入 html 文件 - 任何帮助将不胜感激

通过在 java 中传递值将结果写入 html 文件 - 任何帮助将不胜感激

POPMUISE 2022-12-28 09:47:21
放置的报告已经覆盖了我想在带有结果的表格中打印的所有结果但是当我打印它时覆盖了结果并且它只打印一行正在从其他类文件传递值以打印结果我试过使用 Buffered writer 来编写代码public void createTestSummaryHTMLTable(String sample,String samp[le,String sample,String passedText,String status) throws IOException {    File file=new File("C:\\AutomationResults\\Forms\\",foldername);    if(!file.exists()){        file.mkdir();}      final String FILE_PATH =(file+"/"+formname);    //final String FILE_PATH = "C:\\AutomationResults\\Forms\\"+formname;    final String FILE_EXTENSION = "html";    DateFormat df = new SimpleDateFormat("yyyyMMdd"); // add S if you need milliseconds    String filename = FILE_PATH + "_"+ df.format(new Date()) + "." + FILE_EXTENSION;    File file2 = new File(filename);    // if file doesnt exists, then create it    if (!file2.exists()) {        file2.createNewFile();    }    BufferedWriter html = new BufferedWriter(new FileWriter(file2));    html.write("<html><body bgcolor='#E6E6FA'><table border=1 cellpadding=0>");    html.write("<title>Automation Summary report </title>");    html.write("<br>");    html.write("<center><strong><em>Batch report heading</em></strong>");    html.write("<span style ='float; right;position:relative;right:30px;font weight:bold'>"+new Date().toString()+"</span>");    html.write("<tr>");    addlabel1(html,"Test Step Name");    addlabel1(html,"Test Step Description");    addlabel1(html,"Status");       addlabel1(html,"Screenshot");    html.write("</tr>");    html.write("<tr>");    html.newLine();    addlabel(html,sample);    addlabel(html,sample);    String status1 = "Passed";    if (status1.equals(status))    {        html.write("<td width='20'style='color:#00F00;font-weight:bold;text-align:center'>"+status+"</td>");    } else {        html.write("<td width='20'style='color:#A52A2A;font-weight:bold;text-align:center'>"+status+"</td>");    }    addlabel(html,"Screenshot");    html.write("</tr>");    html.write("</table></body></html>");    html.close();}
查看完整描述

3 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

您可以尝试添加一个布尔参数,以FileWriter帮助指定它是应该附加到现有内容还是覆盖现有内容。如果传递 true,它将以追加模式打开文件进行写入。

请更改此行:

BufferedWriter html = new BufferedWriter(new FileWriter(file2));

对此:

BufferedWriter html = new BufferedWriter(new FileWriter(file2, true));


查看完整回答
反对 回复 2022-12-28
?
千巷猫影

TA贡献1829条经验 获得超7个赞

实际结果

为每一行显示标题我不需要为所有行显示标题结果我得到的是预期结果

标题应该只显示一次


查看完整回答
反对 回复 2022-12-28
?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

哦,我想我猜到了你的问题。写入的文件是一个长行。

那是因为 write 只写你给它的东西。您需要在所有字符串的末尾放置一个 \n。

或者,使用 Printwriter 的 println 方法输出到您的文件。


查看完整回答
反对 回复 2022-12-28
  • 3 回答
  • 0 关注
  • 163 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信