0

My code reading a new file in every 5 sec. From that file I am fetching the status like "pass" and "fail" and writing into a html file. Everything is working fine but my problem is I want previous result also but it is not happening.

Suppose I am reading file1, then I am writing the result in html file and again I read another file2, the result is also writing into same html file. Can anybody please guide me how to do this? I have tried several time but it is not working. Below is the method where I want to implement the same. If needed I will put the whole code also.

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;
        }


      } 

Thanks in advance.

JP Dutta
  • 123
  • 1
  • 13
  • To make this clear: do you want to append file data every 5s to the same html file? And please post your expected output and the output you get along with your code. – CrazySabbath Sep 15 '17 at 09:53
  • no I am not using...I want both the result.previous result is overriding by current result @Mohit Tyagi – JP Dutta Sep 15 '17 at 09:57
  • I want file1 and file2's result in one html file. @CrazySabbath – JP Dutta Sep 15 '17 at 10:22
  • Possible duplicate of [How to append text to an existing file in Java](https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – TDG Sep 15 '17 at 12:17

1 Answers1

0

You need to change the way you initiate FileWritter to append, like so:

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\">");        
}
CrazySabbath
  • 1,274
  • 3
  • 11
  • 33