Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java OutputStream to Multiple files [duplicate]

Tags:

java

file

file-io

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.

like image 575
Steve Avatar asked Oct 21 '14 14:10

Steve


1 Answers

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")))))
like image 87
Fabian Barney Avatar answered Nov 16 '22 07:11

Fabian Barney