I have an OutputStream, and I'd like to (on a conceptual level) broadcast it to multiple files. So for instance, if a byte shows up in the stream, I want that to get written to files A, B, and C.
How can I accomplish this using only one stream? Preferably with a pure Java solution.
You can use Apache Commons IO TeeOutputStream for this purpose.
This OutputStream proxies all bytes written to it to two underlying OutputStreams.
You can use multiple TeeOutputStreams
in a chain when you want to write to 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")))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With