Q:

How to set streams flushed instantaneously beforehand

I'm writing a package for unit testing in Netbeans IDE. Now I'm printing flow of execution statements to System.out while error statements to System.error. Now, since they are two different streams they are printing out of order. I mean they are not printing synchronized. I know flush method makes the statements printed instantaneously but writing this after every such statement is tedious. How to configure beforehand streams to flush immediately. May be at the start of unit testing?.
I want this, because I want to take the advantage of two different colors that Netbeans shows for two streams' of output.
Also, any wrong points in my approach & suggestions for betterment are most welcome.

A:

There could be another possibility, so give it a try:
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class SystemOut {
    public static void main(String[] args) {
        System.out.println("Hi");

        final OutputStream newOs = new OutputStream() {
            FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);

            @Override
            public void write(int b) throws IOException {
                this.fdOut.write(b);
            }
        };

        System.setOut(new PrintStream(newOs));
        System.out.println("Hi, again");
    }
}

The idea is to replace the existing buffered output stream with an unbuffered one.

