- 把报错的异常信息输出到文件:
- class ExceptionInfo
- {
- public static void main(String[] args)
- {
- try
- {
- int [] arr = new int [2];
- System.out.println(arr[3]);
- }
- catch (Exception e)
- {
- try
- {
- //添加打印日期
- Date d = new Date();
- //格式化日期
- SimpleDateFormat sdf = new
- SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
- String s = sdf.format(d);
- //强制改变输出标准设备 修改成
- 输出到文件
- PrintStream ps = new
- PrintStream("ExceptionInfo.txt");
- ps.println(s);
- System.setOut(ps);
- }
- catch (IOException e1)
- {
- throw new RuntimeException("
- 建立日志文件失败!");
- }
-
- e.printStackTrace(System.out);
- }
- }
- }
复制代码
|
|