Transfers all bytes that can be read from one stream to another stream. : OutputStream « File « Java Tutorial






import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {


  /**
   * Transfers all bytes that can be read from <tt>in</tt> to <tt>out</tt>.
   *
   * @param in The InputStream to read data from.
   * @param out The OutputStream to write data to.
   * @return The total number of bytes transfered.
   */
  public static final long transfer(InputStream in, OutputStream out)
    throws IOException
  {
    long totalBytes = 0;
    int bytesInBuf = 0;
    byte[] buf = new byte[4096];

    while ((bytesInBuf = in.read(buf)) != -1) {
      out.write(buf, 0, bytesInBuf);
      totalBytes += bytesInBuf;
    }

    return totalBytes;
  }

}








11.13.OutputStream
11.13.1.Byte Counting OutputStream
11.13.2.Memory Byte Array OutputStream
11.13.3.A null output stream. All data written to this stream is ignored.
11.13.4.Provides true Closable semantics ordinarily missing in a {@link java.io.ByteArrayOutputStream}.
11.13.5.Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when finished.
11.13.6.Transfers all bytes that can be read from one stream to another stream.