Can I duplicate or intercept the output stream in Java?

I want to intercept the standard output stream and then copy the contents to another stream, but I also hope to keep the standard output stream as the original. Can I achieve this in Java?

+5
source share
2 answers

You can use something like the example TeeOutputStreamdescribed here Writing your own Java I / O stream classes

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

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

Edit:

Oracle , TeeOutputStream Apache Commons, - .

+5

All Articles