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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解java捕获异常信息存入txt文件示例的相关内容吗,在本文为您仔细讲解java捕获异常的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:捕获异常,下面大家一起来学习吧。

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

复制代码 代码如下:

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();
  }
 }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们