KJW
KJW

Reputation: 15251

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()); spits out the appropriate strings....however myfile.txt is empty !

Upvotes: 3

Views: 687

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 993085

You have not closed 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.

Upvotes: 5

Related Questions