Java InputStream to OutputStream copyStreamToFile(InputStream is, File outputFile)

Here you can find the source of copyStreamToFile(InputStream is, File outputFile)

Description

Copy contents of a stream to a target file.

License

Apache License

Parameter

Parameter Description
is input stream
outputFile target file

Exception

Parameter Description
IOException an exception

Declaration

public static void copyStreamToFile(InputStream is, File outputFile) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedOutputStream;
import java.io.File;

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

public class Main {
    /**//from   w w w  .  j  a v  a  2s. c  o  m
     * Copy contents of a stream to a target file.  Leave the input
     * stream open afterwards.
     * @param is input stream
     * @param outputFile target file
     * @throws IOException
     */
    public static void copyStreamToFile(InputStream is, File outputFile) throws IOException {
        OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile));
        copyStreamToStream(is, os);
        os.close();
    }

    /**
     * Copy contents of an input stream to an output stream.  Leave both 
     * streams open afterwards.
     * @param is input stream
     * @param os output stream
     * @throws IOException
     */
    public static void copyStreamToStream(InputStream is, OutputStream os) throws IOException {
        byte[] buffer = new byte[4096];
        for (int read = is.read(buffer); read != -1; read = is.read(buffer)) {
            os.write(buffer, 0, read);
        }
        os.flush();
    }
}

Related

  1. copyStreamSafely(InputStream in, ByteArrayOutputStream os)
  2. copyStreamToFile(final InputStream stream, final File output)
  3. copyStreamToFile(InputStream input, String outputPath)
  4. CopyStreamToFile(InputStream inputStream, File outputFile)
  5. copyStreamToFile(InputStream inputStream, File outputFile)
  6. copyStreamToFile(InputStream stream, String outputFilePath)
  7. copyStreamToFileBinary(final InputStream stream, final File output)
  8. copyStreamToStream(final InputStream input, final OutputStream output)
  9. copyStreamToStream(InputStream in, OutputStream out)