Do you need System.out.flush() before System.error.print("");

2.4k Views Asked by At

If you use System.err.print("There was an error..."); System.error.flush(); do you need to flush System.out before hand. I know that writing with System.out.print("bla"); and System.error.print("bla"); can cause the unflushed streams to get mixed up.

I'm assuming that you would only need to do this if you know that there wasn't a System.out.println(); or System.out.print("\n"); because a "\n" automatically flushes in java.lang.System, but I just wanted to make sure.

Also, please don't say in the comments "Why didn't you just test it?" as it's a little hard to test this because you are relying on luck, essentially, to see whether the streams don't get flushed properly.

Thanks!

EDIT

Answer:

I guess just use Logger.getLogger(Your_Class.class.getName()).log(Level.WHATEVER, "message"); if there is no exception.

-

If you don't believe me that sout and serr get mixed up...

PROOF (class I just made):

package test;

/**
 *
 * @author dylnmc
 */
public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            if (i % 2 == 0) {
                System.out.println(i);
            } else {
                System.err.println(i);
            }
        }
    }

}

output :

run:
0
1
3
2
4
6
8
10
12
14
16
18
5
7
9
11
13
15
17
19
BUILD SUCCESSFUL (total time: 0 seconds)

If you turn Test.java into jar and run java -jar file/to/jar/Test.jar pause there is no mismatch, though - as some people were saying (because this is a problem in most ide consoles).

-

-

Also, I still could not get the two different streams to flush in Netbeans ide; they were still all over the place.

2

There are 2 best solutions below

3
Gowtham On BEST ANSWER

System.out and System.err are two different streams of data. Writing to one normally does not alter the other. IDEs like Netbeans or Eclipse read both out and err streams and display both in the console window simultaneously. Data read from err stream might be highlighted in red. This makes it appear that output is "mixed up".

So, System.out.flush() will not have any effect on System.err. Instead, you might consider redirecting System.out to a separate stream using System.setOut, System.setErr and print the captured data just before exiting the program.

1
Pierre-Yves Saumont On

System.out.println(String s) and System.err.println(String s) are not atomic operations because they first print the string and then a newline. Atomicity is relative. The two operations (printing the string and printing the newline) are in a synchronized block, so they are atomic form the Java code point of view. However, they are still two separate operations for the underlying system. So, if the console is shared by err and out, the following code:

System.out.println("abc");
System.out.println("def");

will eventually result in "abc", "\n", "def", "\n" being printed in potentially incorrect order. The first newline will always be printed after "abc", and the second newline will always be printed after "def" (due to synchronization) but the sequence "abc", "def" "\n", "\n" is possible.

To avoid this, avoid using println:

System.out.print("abc" + "\n");
System.out.print("def" + "\n");

(Of course, you should use the correct line separator for your system.)