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

前端 未结 5 925
野性不改
野性不改 2020-11-27 20:37

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

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
FileOutputS         


        
5条回答
  •  心在旅途
    2020-11-27 21:30

    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    final FileOutputStream fileStream = new FileOutputStream(new File("/tmp/somefile"));
    OutputStream outStream = new OutputStream() {
    
        public void write(int b) throws IOException {
            byteStream.write(b);
            fileStream.write(b);
        }
    };
    outStream.write("Hello world".getBytes());
    

提交回复
热议问题