After playing with PrintWriter and the files, I began to doubt why sometimes, when I read my files right away, when I create them, inconsistencies arise, for example:
File file = new File("Items.txt"); int loopValue = 10; try { PrintWriter fout = new PrintWriter(file); for (int i = 0; i < loopValue; i++) { fout.print(i + " asdsadas" + System.lineSeparator()); }
If I run this code, I will read the empty file in the console, something like this:
Here is the file:
But if I change loopValue to something like 10000, I will have something like this:
Here is the file: 0 asdsadas 1 asdsadas 2 asdsadas ... ... continues ... 9356 asdsadas 9357 asdsadas 9358 <--- here ends, note that it doesnt end in the value 9999
I know that if I call flush() or close() before reading the file, I can get rid of this problem, but why is this happening? When PrintWriter decide it's time to flush its buffer if I don't say when? and why, when I close or erase PrintWriter , this problem will not happen?
Thanks!
source share