使用核心java生成html报告但以前的结果不存储

时间:2017-09-15 09:47:13

标签: java html arrays jsoup

我的代码每5秒读取一个新文件。从该文件中我获取“pass”和“fail”之类的状态并写入html文件。一切都工作正常但我的问题是我也想要以前的结果,但它没有发生。

假设我正在读取file1,然后我将结果写入html文件并再次读取另一个file2,结果也写入相同的html文件。有人可以指导我怎么做吗?我已经尝试了几次,但它没有用。 下面是我想要实现相同的方法。如果需要,我也将整个代码。

public static String extractText(Reader reader) throws IOException 
        {

            String s[]={
                        "RAP_45 1",
                        "RAP_50 1",
                        "RAP_75 1",
                        "RAP_PayAsYouGo 1",
                        "RAP_Refill_Coupon 1",
                        "RAP_UserPreferences_DefaultMarket 1",
                        "RAP_NonTransitional_PortIn 1",
                        "RAP_Transitional_PortIn 1",
                        "RAP_Activation_Simply_Prepaid_Mexico/Canada 1"
                        };

            StringBuilder sb = new StringBuilder();
            BufferedReader br = new BufferedReader(reader);
            FileWriter writeReport = new FileWriter("C:\\Users\\jdutta\\files\\ResultSummary.html");
            BufferedWriter bw = new BufferedWriter(writeReport);
            String line;
            bw.write("<html><Body  font=\"14px/1.4 Georgia\" ><br><table border=\"2%\" width=\"50%\"><h3 >Summary:</h3><tr bgcolor=\"black\">");        
            bw.write("<div border=\"1px solid\"  color=\" #ccc\"   padding=\"50px\"> <th><font color=\"white\">Test</th> <th><font color=\"white\">Passed</th><th><font color=\"white\">Failed</th> <th><font color=\"white\">Execution time</th>");

            while ( (line=br.readLine()) != null) 
            {

            sb.append(line);    
            }
            String textOnly = Jsoup.parse(sb.toString()).text();
            int pass=0;
            int fail=0;
            for(int i=0;i<s.length;i++) 
                {
                    String[] s1=s[i].split(" ");
                    System.out.println("s1.........."+s1[1]);

                    if(textOnly.contains(s1[0]))
                        {
                            if(textOnly.contains(s[i])&&s1[1].equals("1"))
                                {
                                    System.out.println(s[i]+"pass");
                                    bw.write("<tr><td>" + s1[0] +  "</td><td>" + "1" + "</td><td>" + "0" + "</td><td>"+ "200sec" + "</td></tr>");
                                    ++pass;
                                }

                            else{
                                    System.out.println("fail"+s1[0]);
                                    bw.write("<tr><td>" + s1[0] +  "</td><td>" + "0" + "</td><td>" + "1" + "</td><td>"+ "200sec" + "</td></tr>");
                                    ++fail;
                                }
                        }
                    else{
                            bw.write("<tr><td>" + s1[0] +  "</td><td>" + "-" + "</td><td>" + "-" + "</td><td>"+ "-" + "</td></tr>");
                        }

                }

                bw.write("<tr><th>" + "Total" +  "</th><th>" + pass + "</th><th>" + fail + "</th></tr>");

                bw.write("</tr></table></Body></html>");
                bw.close();
                return  textOnly;
        }


      } 

提前致谢。

1 个答案:

答案 0 :(得分:0)

您需要更改发起FileWritter的方式,如下所示:

String htmlFilePath = "C:\\Users\\jdutta\\files\\ResultSummary.html";
File htmlFile = new File(filePathString);
boolean append = htmlFile.isFile() && htmlFile.exists();

FileWriter writeReport = new FileWriter(htmlFilePath, append); //notice append

//I assume you dont need to append html header to existing file
if (!append) {
    bw.write("<html><Body  font=\"14px/1.4 Georgia\" ><br><table border=\"2%\" width=\"50%\"><h3 >Summary:</h3><tr bgcolor=\"black\">");        
}