Java OutputStream for multiple files

I have an OutputStream and I would like (conceptually) to stream it to multiple files. So, for example, if a byte appears in the stream, I want that to be written to files A, B and C.

How can I do this using only one thread? Preferably with a pure Java solution.

+3


source to share


1 answer


You can use Apache Commons IO TeeOutputStream for this purpose. This OutputStream proxies all bytes written to it into two base OutputStreams. You can use multiple TeeOutputStreams

in a chain if you want to write more than two OutputStreams at once.



OutputStream out = new TeeOutputStream(new FileOutputStream(new File("A")), new TeeOutputStream(new FileOutputStream(new File("B")), new FileOutputStream(new File("C")))))

      

+3


source







All Articles