Java Tip 14: Redirecting standard streams in Java

Change input, output, and error streams

This week we show how to redirect the standard system-provided input, output, and error streams.

The stream redirection demo application shows the various steps to take to redirect all three of the standard streams to/from regular files.

The application presumes a file called exists. It reads that file in place of the standard input stream and prints it to the original System.out standard output stream.

The application will redirect the System.out output stream and the System.err standard error stream to the Redirect.out and Redirect.err files, respectively.

Armed with this knowledge we can now go wild and write our own subclasses of the InputStream and PrintStream to add all sorts of fun behavior -- like printing things out to both the original console and a file, or maybe down a socket connection, or...

Subsisting on caffeine, sugar, and too little sleep, John D. Mitchell has been consulting for most of the last nine years, and developed PDA software in OO assembly language at Geoworks. He funds his Java addiction by writing compilers, Tcl/Tk, C++, and Java systems. He co-authored the hot new Java book Making Sense of Java and is currently developing a Java compiler.

Learn more about this topic