是否可以告诉 Eclipse 在控制台中只显示错误?
这可能很有用,因为我正在使用使用输出来显示不需要的数据的外部库。我知道有一个按钮“标准错误更改时显示控制台”,但是,我正在寻找一个过滤器。
问候。
您可以使用 System.setOut 和 System.setErr 函数将 System.out 和/或 System.err 重定向到另一个 OutputStream。这样您就可以创建自己的“控制台视图”或自己处理输出。如果您实现自定义 OutputStream 类,您还可以将数据发送到原始 OutputStream(原始 System.out 或 System.err)。
PrintStream oldOut;
oldOut = System.out;
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
// handle the "b" or
// send it to old stdout:
oldOut.write(b);
}
}));
// do what you want with the stream and afterwards
System.setOut(oldOut);
另请注意,如果您在 UI 线程中执行此操作,则可能会出现问题。对我来说,应用程序总是在之后挂起。
如果您重定向这些流,您可以决定是否将其转发到标准输出/错误。
Eclipse(Console
视图)显示了两种不同的输出:标准输出(System.out
)和错误输出(System.err
)。Eclipse 本身无法区分程序运行记录的实际错误/异常,以及将版权/许可信息写入错误流的外部库。