This question already has answers here:
How to write data to two java.io.OutputStream objects at once?

(5个答案)


5年前关闭。




我有一个OutputStream,并且我想(在概念上)将其广播到多个文件。因此,例如,如果流中出现一个字节,我希望将其写入文件A,B和C。

如何仅使用一个流完成此操作?最好使用纯Java解决方案。

最佳答案

为此,您可以使用Apache Commons IO TeeOutputStream
此OutputStream将写入它的所有字节代理到两个基础OutputStreams。
如果要一次写入两个以上的OutputStream,可以在一个链中使用多个TeeOutputStreams

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

09-07 23:45
查看更多