FileDocCategorySizeDatePackage
TeePrintStream.javaAPI DocExample2820Sun Feb 08 17:57:30 GMT 2004None

TeePrintStream

public 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.

author
Ian F. Darwin, http://www.darwinsys.com/
version
$Id: TeePrintStream.java,v 1.5 2004/02/08 23:57:29 ian Exp $

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 booleancheckError()
Return true if either stream has an error.

		return parent.checkError() || super.checkError();
	
public voidclose()
Close both streams.

		parent.close();
		super.close();
	
public voidflush()
Flush both streams.

		parent.flush();
		super.flush();
	
public static voidmain(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 voidwrite(int x)
override write(). This is the actual "tee" operation.

		parent.write(x);	// "write once;
		super.write(x);		// write somewhere else."
	
public voidwrite(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."