Java输出控制台错误消息到文件?

Dan*_*Man 7 java error-handling console text-files

我有一段简单的代码将控制台文本输出到Java中的文本文件:

PrintStream out = new PrintStream(new FileOutputStream("test2_output.txt"));
System.setOut(out);
Run Code Online (Sandbox Code Playgroud)

但是,我要求此文本文件包含在控制台中生成的错误消息,但不包括它们.

我该怎么做呢?

icy*_*com 11

加:

System.setErr(out);
Run Code Online (Sandbox Code Playgroud)

在末尾.