2013-10-24 161 views
1

我已經編寫了一個程序,該程序將源代碼刪除了兩次,並使用檢索到的數據中的特定信息創建了一個CSV。我的問題是,當我去保存第二部分數據時,而不是添加到創建的CSV中時,它會用新信息覆蓋它。我已經提到link,但它使用了不同的類。我的代碼是:添加到文件而不是覆蓋

public static void scrapeWebsite() throws IOException { 


    final WebClient webClient = new WebClient(); 
    final HtmlPage page = webClient.getPage(s); 
    originalHtml = page.getWebResponse().getContentAsString(); 
    obtainInformation(); 
    originalHtml = ""; 
    final HtmlForm form = page.getForms().get(0); 
    final HtmlSubmitInput button = form.getInputByValue(">"); 
    final HtmlPage page2 = button.click(); 
    try { 
     synchronized (page2) { 
     page2.wait(1000); 
     } 
    } 
    catch(InterruptedException e) 
    { 
     System.out.println("error"); 
    } 
    originalHtml = originalHtml + page2.refresh().getWebResponse().getContentAsString(); 
    obtainInformation(); 
    } 

    public static void obtainInformation() throws IOException { 

    PrintWriter docketFile = new PrintWriter(new FileWriter("tester3.csv", true)); 

//創建csv文件。 (名稱必須改變,重寫刪除文件) originalHtml = originalHtml.replace( '「', '*'); INT I = 0;

//While loop runs through all the data in the source code. There is (14) entries per page. 
    while(i<14) { 
     String plaintiffAtty = "PlaintiffAtty_"+i+"*>"; //creates the search string for the plaintiffatty 
     Pattern plaintiffPattern = Pattern.compile("(?<="+Pattern.quote(plaintiffAtty)+").*?(?=</span>)");//creates the pattern for the atty 
     Matcher plaintiffMatcher = plaintiffPattern.matcher(originalHtml); // looks for a match for the atty 

     while (plaintiffMatcher.find()) { 
     docketFile.write(plaintiffMatcher.group().toString()+", "); //writes the found atty to the file 
     } 
     i++; 
    } 
    docketFile.close(); //closes the file 
    } 
} 

相信的變化將在要進行第二方法。

回答

3

PrintWriter你應該引用一個FileWriter與追加構造布爾設置爲true構成。

例如

new PrintWriter(new FileWriter("myfile.csv", true)); 

請注意Javadoc爲FileWriter。您的編碼規範:

用於編寫字符文件的便捷類。這個類的構造函數 假定默認字符編碼和默認的 字節緩衝區大小是可以接受的。要自己指定這些值, 在FileOutputStream上構造一個OutputStreamWriter。

+0

的偉大工程。編輯代碼以反映正確的更改。 – Ctech45

相關問題