The f
comes from the left over string of br.append("fffff");
which was written in the file by the BufferedWriter
.
Since both BufferedWriter
and PrintWriter
write to the same file, the contents written by the PrintWriter
overwrite the contents written by the BufferedWriter
.
But seems the no. of bytes written by PrintWriter
fall short by 1 to completely overwrite the data written by BufferedWriter
and thus you get the f
.
If you change this br.append("fffff");
to br.append("ffffg");
, you can see that the g
is now left over. Alternatively, changing pw.println("folks");
to pw.println("folks1");
will show that the previously written data is now completely overwritten by the PrintWriter
.
All this confusion is because of having 2 different writers for the same file object which is the cause of the problem. As @Boris pointed out, have just 1 writer for a file object.
Note: Another interesting thing to test out would be to move the second br.flush();
after the pw.flush();
.
// br.flush(); // moved from here
pw.println("howdy"); // write the data
pw.println("folks");
pw.flush();
br.flush(); // to here