Reputation: 953
I am using an OutputStream
and a PrintWriter
. Here's my code:
OutputStream os = System.out;
PrintWriter writer = new PrintWriter(os, false);
writer.println("Hell");
writer.println("Hello");
writer.println("Hello");
writer.println("Hello");
writer.println("Hello");
writer.println();
os.write("45\n".getBytes(), 0, "45\n".getBytes().length);
writer.println();
writer.flush();
os.flush();
The output is:
45
Hello
Hello
Hello
Hello
Hello
Why is 45
printed before Hello
's, even though the PrintWriter
is flushed before the OutputStream
?
Edit: Please correct me If I am wrong about flushing.
Upvotes: 2
Views: 54
Reputation: 2776
Well, apperantly when calling os.write()
you get you to a PrintStream.write()
:
Which has autoFlush = true
on default, whereas the PrintWriter is initialized with autoFlush=false
.
Upvotes: 2