Java基础之IO流,打印异常信息到流(文件)中
import java.io.*;
import java.text.*;
import java.util.*;
class ExceptionInfo
{
public static void main(String[] args)
{
try
{
int[] array = new int[2];
System.out.println(array[3]);
}
catch(Exception e)
{
try
{
System.setOut(new PrintStream(new File("err.txt")));
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S E").format(new Date());
PrintStream ps = new PrintStream(System.out);
ps.println(date);
}
catch(IOException ioe)
{
throw new RuntimeException("日志文件创建失败!");
}
e.printStackTrace(System.out);
}
}
}
import java.text.*;
import java.util.*;
class ExceptionInfo
{
public static void main(String[] args)
{
try
{
int[] array = new int[2];
System.out.println(array[3]);
}
catch(Exception e)
{
try
{
System.setOut(new PrintStream(new File("err.txt")));
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S E").format(new Date());
PrintStream ps = new PrintStream(System.out);
ps.println(date);
}
catch(IOException ioe)
{
throw new RuntimeException("日志文件创建失败!");
}
e.printStackTrace(System.out);
}
}
}