2021-12-10_改变标准输入输出设备_异常的日志信息
作者:互联网
1. P247改变标准输入输出设备
System.setIn(new FileInputStream("PersonDemo.java")); System.setOut(new PrintStream("zz.txt"));
2. P248:异常的日志信息
import java.io.*; import java.util.*; import java.text.*; class ExceptionInfo { public static void main(String[] args) { try { int[] arr = new int[2]; System.out.println(arr[3]); } catch (Exception e) { // e.printStackTrace(); // e.printStackTrace(System.out); try { Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String s = sdf.format(d); PrintStream ps = new PrintStream("exception.log"); // ps.write(s.getBytes()); ps.println(s); System.setOut(ps); } catch (IOException ex) { throw new RuntimeException("日志文件创建失败"); } e.printStackTrace(System.out); } } } // log4j
3. 做了problem484、problem9。代码较简单,这里不记录了。
标签:10,12,java,PrintStream,ps,System,2021,new,out 来源: https://www.cnblogs.com/pingfanliliang/p/15670030.html