重定向输出流实现程序日志

import java.io.FileNotFoundException;
import java.io.PrintStream;

public class RedirectOutputStream {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            PrintStream out = System.out;
            PrintStream ps = new PrintStream("./log.txt");
            System.setOut(ps);
            int age = 18;
            String nameString = "Tony";
            String string = "This is a "+age+" boy,he name is "+nameString;
            System.out.println(string);
            System.setOut(out);
            System.out.println("oK,please look the log.txt.");
        }catch(FileNotFoundException exception){
            exception.printStackTrace();
        }
    }

}

你可能感兴趣的:(重定向输出流实现程序日志)