//IO流以日志文件记录异常
import java.io.*;//异常
import java.util.*;//工具
import java.text.*;//格式化
class ExceptionInfo
{
public static void main(String[] args) throws IOException
{
try
{
int[] arr= new int[2];
System.out.println(arr[3]);
}
catch (Exception e)
{
try
{
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(d);
PrintStream ps = new PrintStream("exeception.log");
//ps.write(d.toString().getBytes());//toString把字符串打印,getBytes()是将一个字符串转化为一个字节数组,
ps.println(s);//把Date d字符串打印
System.setOut(ps);
}
catch (IOException ex)
{
throw new RuntimeException("日志文件创建失败");
}
e.printStackTrace(System.out);
}
}
}
//log4j 日志是应用软件中不可缺少的部分,Apache的开源项目Log4j是一个功能强大的日志组件,提供方便的日志记录
IO流以日志文件记录异常
最新推荐文章于 2023-05-09 18:39:19 发布