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 }

 

相关文章:

  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2021-10-15
  • 2021-06-04
猜你喜欢
  • 2022-12-23
  • 2021-08-22
  • 2021-09-18
  • 2021-10-13
  • 2021-08-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案