Copy in stream to an out stream : Copy « File Input Output « Java






Copy in stream to an out stream

 

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

public class Utils {


  /**
   * Copy in stream to an out stream
   * 
   * @param in
   * @param out
   * @throws IOException
   */
  public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
      byte[] buffer = new byte[1024];
      int len = in.read(buffer);
      while (len >= 0) {
          out.write(buffer, 0, len);
          len = in.read(buffer);
      }
      in.close();
      out.close();
  }

}

   
  








Related examples in the same category

1.Copies file contents from source to destination
2.Copies the contents of the given InputStream to the given OutputStream
3.Copy Pipe
4.Copy any input stream to output file
5.Copy any input stream to output stream
6.Copy file and directory
7.Utility methods for file and stream copying
8.Copies all data from an input stream to an output stream.
9.copy Completely (InputStream input, OutputStream output)
10.copy Completely (Reader input, Writer output)
11.copy Completely(URI input, URI output)
12.Copies the InputStream into the OutputStream, until the end of the stream has been reached.
13.Copies the InputStream into the OutputStream, until the end of the stream has been reached. This method uses a buffer of 4096 kbyte.
14.Copies the contents of the Reader into the Writer, until the end of the stream has been reached.
15.Copies the contents of the Reader into the Writer, until the end of the stream has been reached. This method uses a buffer of 4096 kbyte.
16.Copy a file and user buffer
17.Copy a directory and all of its contents.
18.Copy chars from a Reader to a Writer.
19.Copy the source file system structure into the supplied target location.