javastreamjava-8printstream

Why is flushing not working on System.err?


I'm developing a main class that writes potential exceptions by means of e.printStackTrace() and which finishes with

System.err.flush();
System.out.println("\n>>> EXITING...");

The issue is that when exception trace is big, my exiting message is still printed in the middle of all the error sermon.

What I am doing wrong?


Solution

  • The System.err and System.out are two separate streams. Even if you flush the output, that just means all the characters have been sent to the o/s. The terminal(?) reading both streams might still interleave the two streams when it displays them.

    One remedy might be to tie the streams together, effectively directing the System.err stream to be written to System.out, but you will lose any colouring etc that distinguishes the two streams when displayed.