TeePrintStreampublic class TeePrintStream extends PrintStream TeePrintStream tees all PrintStream operations into a file, rather
like the UNIX tee(1) command. It is a PrintStream subclass. The
expected usage would be something like the following:
...
TeePrintStream ts = new TeePrintStream(System.err, "err.log");
System.setErr(ts);
// ...lots of code that occasionally writes to System.err...
ts.close();
...
I only override Constructors, the write(), check() and close() methods,
since any of the print() or println() methods must go through these.
Thanks to Svante Karlsson for help formulating this. |
Fields Summary |
---|
protected PrintStream | parent | protected String | fileName |
Constructors Summary |
---|
public TeePrintStream(PrintStream orig, OutputStream os, boolean flush)Construct a TeePrintStream given an existing PrintStream,
an opened OutputStream, and a boolean to control auto-flush.
This is the main constructor, to which others delegate via "this".
super(os, true);
fileName = "(opened Stream)";
parent = orig;
| public TeePrintStream(PrintStream orig, OutputStream os)Construct a TeePrintStream given an existing PrintStream and
an opened OutputStream.
this(orig, os, true);
| public TeePrintStream(PrintStream os, String fn)
this(os, fn, true);
| public TeePrintStream(PrintStream orig, String fn, boolean flush)
this(orig, new FileOutputStream(fn), flush);
|
Methods Summary |
---|
public boolean | checkError()Return true if either stream has an error.
return parent.checkError() || super.checkError();
| public void | close()Close both streams.
parent.close();
super.close();
| public void | flush()Flush both streams.
parent.flush();
super.flush();
| public static void | main(java.lang.String[] args)A simple test case.
TeePrintStream ts = new TeePrintStream(System.err, "err.log", true);
System.setErr(ts);
System.err.println("An imitation error message");
ts.close();
| public void | write(int x)override write(). This is the actual "tee" operation.
parent.write(x); // "write once;
super.write(x); // write somewhere else."
| public void | write(byte[] x, int o, int l)override write(). This is the actual "tee" operation.
parent.write(x, o, l); // "write once;
super.write(x, o, l); // write somewhere else."
|
|