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。代码较简单,这里不记录了。