Java InputStream Copy to File copyStreamToFile(InputStream stream, File file)

Here you can find the source of copyStreamToFile(InputStream stream, File file)

Description

Copies the data from the InputStream to a file, then closes both when finished.

License

BSD License

Parameter

Parameter Description
stream a parameter
file a parameter

Exception

Parameter Description

Declaration

public static void copyStreamToFile(InputStream stream, File file) throws IOException 

Method Source Code

//package com.java2s;
// BSD License (http://www.galagosearch.org/license)

import java.io.File;

import java.io.FileOutputStream;

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

public class Main {
    /**//  w ww. j a v  a 2s.c o m
     * Copies the data from the InputStream to a file, then closes both when
     * finished.
     * 
     * @param stream
     * @param file
     * @throws java.io.IOException
     */
    public static void copyStreamToFile(InputStream stream, File file) throws IOException {
        FileOutputStream output = new FileOutputStream(file);
        final int oneMegabyte = 1 * 1024 * 1024;
        byte[] data = new byte[oneMegabyte];

        while (true) {
            int bytesRead = stream.read(data);

            if (bytesRead < 0) {
                break;
            }
            output.write(data, 0, bytesRead);
        }

        stream.close();
        output.close();
    }
}

Related

  1. copyStreamToFile(InputStream inputStream, File destFile)
  2. copyStreamToFile(InputStream pInputStream, File pFile)
  3. copyStreamToFile(InputStream source, File target)
  4. copyStreamToFile(InputStream stream, File destFile)
  5. copyStreamToFile(InputStream stream, File destFile, long fileTime)