Java - 捕获System.err.println或捕获PrintStream [英] Java - Capturing System.err.println or Capturing a PrintStream

查看:175
本文介绍了Java - 捕获System.err.println或捕获PrintStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java新手问题:

我需要捕获第三方组件写入printStream的文本。

I need to capture the text being written to a printStream by a 3rd party component.

PrintStream默认为System.err,但可以更改为另一个PrintStream。

The PrintStream is defaulted to System.err, but can be changed to another PrintStream.

通过文档查看,我找不到一个简单的方法将PrintStream的内容指向字符串编写器/缓冲区。

Looking through the docs, I couldn't find an easy way to direct the contents of a PrintStream to a string writer / buffer.

有人可以协助吗?

推荐答案

PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut);
System.setOut(new PrintStream(pipeOut));
// now read from pipeIn

这篇关于Java - 捕获System.err.println或捕获PrintStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆