简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM