How to write data to two java.io.OutputStream objects at once?

daveslab picture daveslab · Nov 2, 2011 · Viewed 24.6k times · Source

I'm looking for magical Java class that will allow me to do something like this:

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
FileOutputStream fileStream = new FileOutputStream(new File("/tmp/somefile"));

MultiOutputStream outStream = new MultiOutputStream(byteStream, fileStream);

outStream.write("Hello world".getBytes());

Basically, I want tee for OutputStreams in Java. Any ideas?

Thanks!

Answer