JAva: why is nothing written in 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 corresponding lines .... however myfile.txt is empty!

+3
source share
1 answer

You have not closed outFile:

outFile.close();

Closing a file flushes any buffered output and writes it to a file on disk. Do this after you finish writing everything to a file.

+5
source

Source: https://habr.com/ru/post/1760850/


All Articles