简体   繁体   English

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

[英]JAva: why isn't anything being written to my txt file?

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()); 的System.out.println(link.getText()); spits out the appropriate strings....however myfile.txt is empty ! 吐出适当的字符串......但是myfile.txt是空的!

You have not closed outFile : 你还没有关闭outFile

outFile.close();

Closing the file flushes any buffered output and writes it to the file on disk. 关闭文件会刷新任何缓冲的输出并将其写入磁盘上的文件。 Do this after you have finished writing everything to the file. 完成将所有内容写入文件后执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM