异常直接写入文件
package test.log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class ExceptionLog {
public static void main(String[] args) throws IOException {
File file = new File("D:/Java Code/TestFirstProject/src/test/log/exceptionlog.txt");
if(!file.exists()) {
file.createNewFile();
}
PrintStream stream = null;
try {
stream = new PrintStream(new FileOutputStream(file, true));
int i=1/0;
} catch (Exception e) {
e.printStackTrace(stream);
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1d0cf4caefc7bd328c13a3871d0ef74f.png)