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:
As before, the application presumes that a file called Redirect.in exists. It reads that file in place of the System.in 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
Redirect.java. 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 System.in 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.