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