Java Tip 33: Redirecting streams revisited

Learn how to redirect the standard streams in Java 1.1

In Java Tip #14, "Redirecting streams," we showed you how to redirect the standard system-provided input, output, and error streams using Java 1.0. Now we'll modernize our toolbox to redirect streams in Java 1.1.

The stream redirection demo application shows the various steps you can take to redirect all three of the standard streams to or from regular files using Java 1.1.

The java.lang.System API was changed in Java 1.1 such that the in, out, and err streams are now final. Luckily, the API was also extended to include methods to change those streams: setIn(), setOut(), and setErr(), respectively.

As before, the application presumes that 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.

Note that you will get warnings when compiling These warnings are generated in Java 1.1 since the use of the PrintStream constructors have been deprecated. PrintStream has mostly been superseded by the PrintWriter class, but PrintStream is still used for the and System.out streams. So, just ignore those deprecation warnings for this particular use of PrintStream.

Thanks to Jeff Rhyason for prompting us for this update.

Alternately as employee, consultant, and principal of his own company, John has invested the last ten years in developing cutting-edge computer software and advising other developers. He has trained other developers and has provided consulting to a number of leading-edge companies on Java technology, Web-based applications, and Internet commerce. John co-authored Making Sense of Java: A Guide for Managers and the Rest of Us and has published articles in programming journals. In addition to writing the Java Tips column for JavaWorld, he moderates the comp.lang.tcl.announce newsgroup.