Java Reference
In-Depth Information
System.setErr(System.out); // merge stderr and stdout to same output file.
It could also be a stream connected to or from another Process you've started (see Running
an External Program from Java ) , a network socket, or URL. Anything that gives you a
stream can be used.
See Also
See Getting Program Output into a Window , which shows how to reassign a file so that it
gets “written” to a text window in a GUI application.
Duplicating a Stream as It Is Written
Problem
You want anything written to a stream, such as the standard output System.out , or the stand-
ard error System.err , to appear there but also be logged in to a file.
Solution
Subclass PrintStream and have its write() methods write to two streams. Then use sys-
tem.setErr() or setOut() , as in Discussion , to replace the existing standard stream with
this “tee” PrintStream subclass.
Discussion
Classes are meant to be subclassed. Here we're just subclassing PrintStream and adding a
bit of functionality: a second PrintStream ! I wrote a class called TeePrintStream , named
after the ancient Unix command tee . That command allowed you to duplicate, or “tee off,” a
copy of the data being written on a “pipeline” between two programs.
The original Unix tee command is used like this: the | character creates a pipeline in which
the standard output of one program becomes the standard input to the next. This often-used
example of pipes shows how many users are logged into a Unix server:
who | wc -l
Search WWH ::




Custom Search