Handle System.err and System.out with Java Examples
In java the three main Streams stdin (standard input) , stdout (standard output) and stderr (standard output error) are handled by default by System.in, Sytem.out and System.err respectively. Sometimes we may need to change the output according to our needs, this can be done in many ways such us using the OS to tell what stream we need to use or using java to set what streams we want to be used.
In these examples is shown how to deal with these streams in different sceenarios.
Redirecting streams to file from console in Unix
java MyExample > output.log 2>error.log
Redirecting Output Streams to Files
This example redirects both output streams to 2 different files. It produces an ArithmeticException dividing by 0 and the error trace will be stored in the stderr.log. The results won't be shown in the console.
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; public class OutPuts { public static void main(String[] args) { try { FileOutputStream fout = new FileOutputStream("stdout.log"); FileOutputStream ferr = new FileOutputStream("stderr.log"); PrintStream out = new PrintStream(fout); PrintStream err = new PrintStream(ferr); System.setOut(out); System.setErr(err); out.println("trace console 1"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } System.out.println("trace console 2"); System.out.println(100/0); } }
Redirecting Output Error Stream to the Standard Out Stream.
public class OutPuts { public static void main(String[] args) { System.setErr(System.out); System.out.println("trace console"); System.out.println(100/0); } }
Redirecting Output Streams to Files and Printing the result in the console.
In this example a "proxy" class I used, so that every time the print or println methods are called its output will be printed by the console and also stored the specified files.
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; public class OutPuts { public static void main(String[] args) { System.setOut(new ProxyPrintStream(System.out, "stdout.log")); System.setErr(new ProxyPrintStream(System.err, "stderr.log")); System.out.println("trace console 1"); System.out.println(100 / 0); } } class ProxyPrintStream extends PrintStream{ private PrintStream fileStream = null; private PrintStream originalPrintStream = null; public ProxyPrintStream(PrintStream out, String FilePath) { super(out); originalPrintStream = out; try { FileOutputStream fout = new FileOutputStream(FilePath,true); fileStream = new PrintStream(fout); } catch (FileNotFoundException e) { e.printStackTrace(); } } public void print(final String str) { originalPrintStream.print(str); fileStream.print(str); } public void println(final String str) { originalPrintStream.println(str); fileStream.println(str); } }
* This example appends traces to the existing file change new FileOutputStream(FilePath,true) for new FileOutputStream(FilePath) to create a new file each time.
Redirecting Output Streams in Log4J
This example prints all the traces ment for the standard outputs to the LOG4J files, so you can make sure that all traces are there.
/** * To change tie the sysOut and sysErr to the log */ public static void tieSystemOutAndErrToLog() { System.setOut(createLoggingProxy(System.out)); System.setErr(createLoggingProxy(System.err)); } public static PrintStream createLoggingProxy(final PrintStream realPrintStream) { return new PrintStream(realPrintStream) { public void print(final String string) { logger.warn(string); } public void println(final String string) { logger.warn(string); } }; }