放置的報告已經覆蓋了我想在帶有結果的表格中打印的所有結果但是當我打印它時覆蓋了結果并且它只打印一行正在從其他類文件傳遞值以打印結果我試過使用 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));

滄海一幻覺
TA貢獻1824條經驗 獲得超5個贊
哦,我想我猜到了你的問題。寫入的文件是一個長行。
那是因為 write 只寫你給它的東西。您需要在所有字符串的末尾放置一個 \n。
或者,使用 Printwriter 的 println 方法輸出到您的文件。
添加回答
舉報
0/150
提交
取消