Source Code : Redirecting Standard Output, and Error

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Redirecting Standard Output, and Error

 
import java.io.FileOutputStream;
import java.io.PrintStream;

public class Main {
  public static void main(String[] argv) throws Exception {
    System.setOut(new LogStream(System.out, new PrintStream(new FileOutputStream("out.log"))));
    System.setErr(new LogStream(System.err, new PrintStream(new FileOutputStream("err.log"))));
    
    System.out.println("output");
    System.err.println("error");
  }
}

class LogStream extends PrintStream {
  PrintStream out;

  public LogStream(PrintStream out1, PrintStream out2) {
    super(out1);
    this.out = out2;
  }

  public void write(byte buf[], int off, int len) {
    try {
      super.write(buf, off, len);
      out.write(buf, off, len);
    } catch (Exception e) {
    }
  }

  public void flush() {
    super.flush();
    out.flush();
  }
}

   
  

Thank with us