2012-02-14 69 views
6

我有一段简单的代码,控制台文本输出到一个文本文件中的Java:Java输出控制台错误消息到文件?

PrintStream out = new PrintStream(new FileOutputStream("test2_output.txt")); 
System.setOut(out); 

不过,我需要这个文本文件中包含在控制台生产,但它们不包括错误消息。

我该怎么做?

回答

9

地址:

System.setErr(out); 

末。

+0

非常感谢。完美的工作。我无法相信我在Google的任何地方都找不到这种说法。 – 2012-02-14 11:53:29

2

还有一个System.setErr()调用重定向stderr。

0

尝试:

PrintStream pst = new PrintStream("Text.txt"); 
System.setOut(pst); 
System.setErr(pst); 
System.out.println("Hello, Finally I've printed output to file..");