异常直接写入文件
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);
}
}
}

版权声明:本文为qq_41841482原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。