Search code examples
javaprintwriter

Java doesn't print each element in txt-file


I have a little understanding question, because the code

PrintWriter out = new PrintWriter("C:\\Users\\...\\Test.txt");
for(int i = 0; i <= 100000; i++)
{
    out.println(i);
}

should write all digits to 100000 in a txt-file, but it stops at 98720.

The question is why is Java doing this?


Solution

  • You might need to flush and close the print writer when you're done with it.

    out.flush();
    out.close();