JAva:为什么没有写入我的txt文件?

时间:2010-08-21 01:52:16

标签: java

public static void getMembers(WebDriver driver, String outname){

    FileWriter outFile = null;
    try {
        outFile = new FileWriter(new File("myfile.txt"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //continue from 900
    for(int i=1;i<=5070;i++){
        driver.get("/username&page="+i);

        List<WebElement> links = driver.findElements(By.xpath("/html/body/div[2]/div/div/form/table[2]/tbody/tr/td/a"));
        for(WebElement link : links){

            if (link.getText() == ""){                  
            }else{
            try {
                outFile.write(link.getText() + "\n");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(link.getText());
            }
        }

    }
}

的System.out.println(link.getText());吐出适当的字符串....但是myfile.txt是空的!

1 个答案:

答案 0 :(得分:5)

您尚未关闭outFile

outFile.close();

关闭文件会刷新所有缓冲的输出并将其写入磁盘上的文件。完成将所有内容写入文件后执行此操作。