欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java捕获异常信息存入txt文件示例

时间:2021-02-04 11:35:09|栏目:JAVA代码|点击:

捕获程序中出现的异常 可用于后期维护的必要性!做简单的测试 !

复制代码 代码如下:

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 int与integer的对比区别

本文标题:java捕获异常信息存入txt文件示例

本文地址:http://www.codeinn.net/misctech/56813.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有