Java InputStream Copy to File copyStream(InputStream copyFrom, File copyTo)

Here you can find the source of copyStream(InputStream copyFrom, File copyTo)

Description

Copy an input stream to a file location

License

Open Source License

Parameter

Parameter Description
copyFrom a parameter
copyTo a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyStream(InputStream copyFrom, File copyTo) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

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

public class Main {
    /**//from ww w . j a  va 2s .c  o  m
     * Copy an input stream to a file location
     * 
     * @param copyFrom
     * @param copyTo
     * @throws IOException
     */
    public static void copyStream(InputStream copyFrom, File copyTo) throws IOException {

        OutputStream to = new FileOutputStream(copyTo);

        copyStream(copyFrom, to);
    }

    /**
     * Copy an input stream to an output stream
     * 
     * @param copyFrom
     * @param copyTo
     * @throws IOException
     */
    public static void copyStream(InputStream copyFrom, OutputStream copyTo) throws IOException {

        byte[] buffer = new byte[1024];
        int length;
        while ((length = copyFrom.read(buffer)) > 0) {
            copyTo.write(buffer, 0, length);
        }

        copyTo.flush();
        copyTo.close();
        copyFrom.close();
    }
}

Related

  1. copyStream(final InputStream in, final File dest)
  2. copyStream(final InputStream is, final File destinationFile)
  3. copyStream(InputStream in, File dest)
  4. copyStream(InputStream in, File dest)
  5. copyStreamIntoFile(File outFile, InputStream is)
  6. copyStreamsToFile(String path, Map streamMap)