SOP

修改 System.out.println() 的输出内容到文件. (改变System.out的标准输出流)

public class ChangeOut {
    public static void main(String args[]) {
        try {
            System.setOut(new PrintStream(new FileOutputStream("log.txt")));
            System.out.println("Now the output is redirected!");
        } catch(Exception e) {}
    }
}

文章来源: 码农每日一题