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

예외 정보를 캡처하여 txt 파일로 저장하는 Java의 더 많은 예를 보려면 PHP 중국어 웹사이트에서 관련 기사를 주목하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.