利用PrintStream字节打印流将异常信息输出到外部文件
程序员文章站
2024-03-04 21:57:54
...
package com.yd.printstream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class PrintStreamTest {
public static void main(String[] args) throws FileNotFoundException {
try {
int[] arr=new int[-1];
} catch (Exception e) {
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日hh:mm:ss SS");
String line = sdf.format(d);
PrintStream ps=new PrintStream(new FileOutputStream("E:/ydxy/后端/练习/se-29/log.txt"));
ps.println(line);
e.printStackTrace(ps);
ps.close();
}
}
}
输出结果如下: