Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 559
Jo Kepler Avatar asked Mar 14 '26 15:03

Jo Kepler


2 Answers

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

out.flush();
out.close();
like image 106
akaIDIOT Avatar answered Mar 16 '26 04:03

akaIDIOT


You need to add some extra code at the end.

// flush the contents of the stream
out.flush();
// close the stream, and release its resources
out.close();
like image 30
Steve Chaloner Avatar answered Mar 16 '26 04:03

Steve Chaloner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!