替换System.out为自定义的PrintStream,使原来应该打印到控制台的字符写入到文件里

import java.io.*;

public class ChangeOut{
    
    public static void main(String[] args){
        try{
            FileOutputStream fos = new FileOutputStream("D:/Java/ChangeOut/test.txt");
            PrintStream ps = new PrintStream(fos);
            System.setOut(ps);
            System.out.println("it's me");
        } catch(IOException e){
            e.printStackTrace();
        } 
    }
}

 

posted @ 2020-03-03 17:08  yxfyg  阅读(303)  评论(1)    收藏  举报