2017-09-15 8 views
0

내 코드가 매 5 초마다 새 파일을 읽습니다. 이 파일에서 "통과"및 "실패"와 같은 상태를 가져 와서 html 파일에 기록합니다. 모든 것이 잘 작동하지만 내 문제는 이전 결과도 필요하지만 그럴 수는 없습니다.코어 자바를 사용하여 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; 
     } 


     } 

미리 감사드립니다.

+0

이 명확하게하려면 : 동일한 html 파일에 파일 데이터마다 5 초를 추가 하시겠습니까? 그리고 예상 출력과 코드 출력을 게시하십시오. – CrazySabbath

+0

아니요 사용하지 않고 있습니다 ... 결과 두 개 모두를 원합니다. 이전 결과가 현재 결과로 대체됩니다 @Mohit Tyagi –

+0

file1과 file2의 결과를 하나의 html 파일에 넣고 싶습니다. @CrazySabbath –

답변

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\">");   
}