集册 Java实例教程 重定向标准输出和错误

重定向标准输出和错误

欢马劈雪     最近更新时间:2020-01-02 10:19:05

456
重定向标准输出和错误


import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;
/* 
 来自 
*nowjava.com - 时代Java*/


public class Main {

  public static void main(String[] args) {

    try {

      // Tee standard output

      PrintStream out = new PrintStream(new FileOutputStream("out.log"));

      PrintStream tee = new TeeStream(System.out, out);


      System.setOut(tee);


      // Tee standard error

      PrintStream err = new PrintStream(new FileOutputStream("err.log"));

      tee = new TeeStream(System.err, err);


      System.setErr(tee);

    } catch (FileNotFoundException e) {

    }/** from n o w j a v a . c o m - 时  代  Java**/


    // Write to standard output and error and the log files

    System.out.println("welcome");

    System.err.println("error");

  }

}


class TeeStream extends PrintStream {

  PrintStream out;


  public TeeStream(PrintStream out1, PrintStream out2) {

    super(out1);

    this.out = out2;

  }


  public void write(byte buf[], int off, 
展开阅读全文