在System.setOut之后写入实际的STDOUT [英] Writing to the real STDOUT after System.setOut

查看:72
本文介绍了在System.setOut之后写入实际的STDOUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拦截System.out和System.err,但是在必要时保持直接写入原始流的功能.

I'm trying to intercept System.out and System.err, but maintain the ability to write to the original streams directly when necessary.

PrintStream ps = System.out;
System.setOut(new MyMagicPrintStream());
ps.println("foo");

不幸的是,System类实现的详细信息意味着在我的示例中,"foo"被发送到了 MyMagicPrintStream 而不是真正的 stdout .

Unfortunately, the details of the System class' implementation means that in my example, "foo" gets sent to MyMagicPrintStream instead of the real stdout.

有人知道如何获取对真实/原始 OutputStreams 的引用吗?

Does anyone know how to get references to the real/original OutputStreams?

谢谢.

PS:否则,这会导致SEO出现StackOverflowError<-.

推荐答案

尝试一下:

PrintStream ps = new PrintStream(new FileOutputStream(FileDescriptor.out))

这篇关于在System.setOut之后写入实际的STDOUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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