Java FileInputStream Copy copyFile(File copyFrom, File copyTo)

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

Description

Copy a file 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 copyFile(File copyFrom, File copyTo) throws IOException 

Method Source Code

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

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

public class Main {
    /**//ww w  . j a v a 2s  . com
     * Copy a file to a file location
     * 
     * @param copyFrom
     * @param copyTo
     * @throws IOException
     */
    public static void copyFile(File copyFrom, File copyTo) throws IOException {

        InputStream from = new FileInputStream(copyFrom);
        OutputStream to = new FileOutputStream(copyTo);

        copyStream(from, to);
    }

    /**
     * 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. copyFile(File aSource, File aDest)
  2. copyFile(File base, String relfilepath, File targetdir)
  3. copyFile(File copyFrom, File copyTo)
  4. copyFile(File destfile, File srcfile)
  5. copyFile(File destFile, File srcFile)
  6. copyFile(File destination, File source)
  7. copyFile(File existingFile, File destFile)