Java FileInputStream Copy copyFile(InputStream in, File to)

Here you can find the source of copyFile(InputStream in, File to)

Description

Copy from an input stream to a file

License

Open Source License

Parameter

Parameter Description
in a parameter
to a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(InputStream in, File to) throws IOException 

Method Source Code

//package com.java2s;
/*/*from ww  w .  java  2 s  .  co m*/
 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of the License "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description: 
 *
 */

import java.io.*;

public class Main {
    /**
     * Copy from an input stream to a file
     * 
     * @param in
     * @param to
     * @throws IOException
     */
    public static void copyFile(InputStream in, File to) throws IOException {
        FileOutputStream out = new FileOutputStream(to);
        int len;
        byte[] buffer = new byte[4096];
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.close();
        in.close();
    }

    /** Copy a single file from 'from' to 'to'
     * 
     * @param from
     * @param to
     * @throws IOException
     */
    public static void copyFile(File from, File to) throws IOException {
        FileInputStream in = new FileInputStream(from);
        copyFile(in, to);
    }
}

Related

  1. copyFile(final String from, final String to)
  2. copyFile(final String sourceFile, final String destinationFile)
  3. copyFile(final String sourceFilePath, final String destFilePath)
  4. copyFile(final String sSource, final String sDest)
  5. copyFile(InputStream in, File dst)
  6. copyFile(InputStream in, String destFile)
  7. copyFile(OutputStream out, InputStream in)
  8. copyFile(String f1, String f2)
  9. copyFile(String fileIn, String fileOut)