java捕获异常信息存入txt文件示例
捕获程序中出现的异常 可用于后期维护的必要性!做简单的测试 !
package helpEntity;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Log {
private File file = null;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public void saveLog(Exception e, String youName) {
try {
String nowPath = null;
nowPath = System.getProperty("user.dir");
String tempPath = null;
this.file = new File(nowPath);
tempPath = this.file.getParent();
if (tempPath == null) {
this.file = new File(nowPath);
}
this.file = new File(tempPath + "" + File.separator + "log.txt");
PrintWriter writer = null;
FileWriter fileWrite = new FileWriter(file, true);
writer = new PrintWriter(fileWrite);
writer.append(System.getProperty("line.separator")
+ new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss")
.format(new Date()) + "__" + youName);
writer.append(System.getProperty("line.separator"));
writer.append(" *************************" + e.toString()
+ "*************************");
writer.append(System.getProperty("line.separator"));
e.printStackTrace(writer);
writer.flush();
writer.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
上一篇:intellij idea隐藏.iml和.idea等自动生成文件的问题
栏 目:JAVA代码
本文标题:java捕获异常信息存入txt文件示例
本文地址:http://www.codeinn.net/misctech/56813.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




