2013-10-24 235 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

相关问题