首頁  >  文章  >  Java  >  java捕獲異常訊息存入txt檔案範例

java捕獲異常訊息存入txt檔案範例

高洛峰
高洛峰原創
2017-01-20 16:43:071504瀏覽

捕獲程序中出現的異常 可用於後期維護的必要性!做簡單的測試 !

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

更多java捕獲異常資訊存入txt檔案範例相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn