// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class TeeOutputStream extends OutputStream {
    private OutputStream os1;
    private OutputStream os2;
    public TeeOutputStream(OutputStream os1, OutputStream os2) {
        this.os1 = os1;
        this.os2 = os2;
    }
    public void write(byte[] b) throws IOException {
        os1.write(b);
        os2.write(b);
    }
    public void write(byte[] b, int off, int len) throws IOException {
        os1.write(b, off, len);
        os2.write(b, off, len);
    }
    public void write(int b) throws IOException {
        os1.write(b);
        os2.write(b);
    }
    public void flush() throws IOException {
        os1.flush();
        os2.flush();
    }
    public void close() throws IOException {
        os1.close();
        os2.close();
    }
    public static void test2() {
        System.out.println("This is test2");
    }
    public static void test1() {
        System.out.println("This is test1");
    }
    public static void main(String[] args) throws IOException {
        System.out.println("normal");
        PrintStream sav = System.out;
        System.setOut(new PrintStream(new TeeOutputStream(System.out, new PrintStream("C:\\work\\tst.out"))));
        test1();
        test2();
        System.setOut(sav);
        System.out.println("normal");
    }
}
