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.