Ege Hurturk
Ege Hurturk

Reputation: 953

Why is OutputStream write method printed before PrintWriter's println method?

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

Answers (1)

Most Noble Rabbit
Most Noble Rabbit

Reputation: 2776

Well, apperantly when calling os.write() you get you to a PrintStream.write():

enter image description here

Which has autoFlush = true on default, whereas the PrintWriter is initialized with autoFlush=false.

Upvotes: 2

Related Questions