带有'%n'的Java'System.err.format'后跟'System.out.println',println打印在中间

Jit*_*Jit 4 java ubuntu

我是Java的新手.

我使用的是Ubuntu 16.04,JDK 8u101,Netbeans8.1.

尝试此代码时:

public static void main(String[] args) {
    System.err.format("1st Line %nPrints At 3rd Line,Shouldn't this be In 2nd Line ");
    System.out.println("Shouldn't this be the third line,prints at 2nd line");
}
Run Code Online (Sandbox Code Playgroud)

输出是:

This Prints At 1st Line 
Shouldn't this be the third line, but prints at 2nd line
This Prints At 3rd Line, Shouldn't this be In 2nd Line
Run Code Online (Sandbox Code Playgroud)

为什么" System.out.println"打印在中间?不应该打印最后.

我最后尝试了" %n"并且System.err.flush()像这样:

System.err.format("1st Line %nPrints At 3rd Line,Shouldn't this be In 2nd Line%n");
System.err.flush();
System.out.println("Shouldn't this be the third line,prints at 2nd line");
Run Code Online (Sandbox Code Playgroud)

还是输出相同.

Ell*_*sch 5

你是不是调用flush()之间println()的调用和System.outSystem.err均(独立)缓冲PrintStream(S).

// and you need a %n on the end to make 3 lines.
System.err.format("1st Line %nPrints At 3rd Line,Shouldn't this be In 2nd Line%n"); 
System.err.flush(); // <-- only needed if the previous write doesn't have
                    //     an implicit flush(); newline (%n) does.
System.out.println("Shouldn't this be the third line,prints at 2nd line");
Run Code Online (Sandbox Code Playgroud)