重定向输出流简单实现程序日志

 

try {
    PrintStream out = System.out;
    PrintStream err = System.err;
    PrintStream ps = new PrintStream("E:/log.txt");
    System.setOut(ps);
    System.setErr(ps);
    System.out.println("这是要写入日志文件的调试信息!");
    System.err.println("这是要写入日志文件的错误信息!");
    System.setOut(out);
    System.setErr(err);
    System.out.println("程序运行完毕,请查看日志文件!");
    System.err.println("我是打印在控制台的错误信息!");
} catch (FileNotFoundException e) {
     // TODO 自动生成 catch 块
     e.printStackTrace();
}
posted @ 2014-01-15 13:33  cRaZy_TyKeIo  阅读(109)  评论(0编辑  收藏  举报