我可以在Java中复制或拦截输出流吗? [英] Could I duplicate or intercept an output stream in Java?

查看:90
本文介绍了我可以在Java中复制或拦截输出流吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拦截标准输出流,然后将内容复制到另一个流,但我也希望保持标准输出流像原始一样。我可以用Java实现吗?

I want to intercept the standard output stream, then copy the content to another stream, but I also hope to keep the standard output stream like the original. Could I achieve that in Java?

推荐答案

您可以使用类似 TeeOutputStream 在此解释编写自己的Java I / O流类

You can use something like the example of TeeOutputStream explained here Writing Your Own Java I/O Stream Classes

基本上你创建一个TeeOutputStream,给它你的流和当前的System.out
然后使用System.setOut和新的流。

Basically you create a TeeOutputStream, give it your stream and current System.out then use System.setOut with the new stream.

写入System.out的任何内容都将写入原始的System.out以及您的流,以便您可以随意使用它。

Anything written to System.out will be written to the original System.out as well as your stream so you can do whatever you want with it

编辑:

Oracle取消了此页面,也可以使用 TeeOutputStream 在不添加任何代码的情况下执行相同操作。

Oracle took off this page, It is also possible to use TeeOutputStream from Apache Commons to do the same thing without adding any code.

这篇关于我可以在Java中复制或拦截输出流吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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