java中将异常写入到txt文本

 

代码如下:

try{
	******
} catch (Exception ex) {
				  try {
					  File file = new File("e:/log.txt");

					  PrintWriter writer = null;
					  FileWriter fileWrite = new FileWriter(String.valueOf(file), true);
					  writer = new PrintWriter(fileWrite);
					  writer.append(System.getProperty("line.separator") + new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date()));
					  writer.append(System.getProperty("line.separator"));
					  writer.append("*************************" + ex.toString() + "*************************");
					  writer.append(System.getProperty("line.separator"));
					  ex.printStackTrace(writer);
					  writer.flush();
					  writer.close();
				  } catch (IOException e) {
					  e.printStackTrace();
				  }
}
posted @ 2015-11-25 09:35  技术宅home  阅读(870)  评论(0编辑  收藏  举报