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

 1 import java.io.FileNotFoundException;
 2 import java.io.PrintStream;
 3 
 4 public class RedirectOutputStream {
 5     public static void main(String[] args) {
 6         PrintStream out=System.out;//保存原来的输出流
 7         try {
 8             PrintStream newStream=new PrintStream("./log.txt");//创建新的输出流
 9             System.setOut(newStream);//设置新的输出流,即为讲输出内容更改到新的输出对象中
10             System.out.println("更改了输出流");
11             System.setOut(new PrintStream(System.out));//还原输出流**原输出流为System.out指向的控制台**而且不能直接使用System。out
12             System.out.println("还原了输出流!!原文件保存到日志文件中");
13         } catch (FileNotFoundException e) {
14             e.printStackTrace();
15         }
16     }
17 }
原文地址:https://www.cnblogs.com/yigenmao/p/6937893.html