Java FileInputStream Copy copyFile(File destination, File source)

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

Description

Copies the contents of one file to another.

License

Open Source License

Parameter

Parameter Description
destination Destination file
source Source file

Declaration

public static void copyFile(File destination, File source)
        throws java.io.IOException 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

public class Main {
    /**//from w  ww. j  av a 2 s. co  m
     * Copies the contents of one file to another.
     * 
     * @param destination
     *            Destination file
     * @param source
     *            Source file
     * 
     * @exception java.io.IOException
     *                Thrown if there is an error copying the file.
     */
    public static void copyFile(File destination, File source)
            throws java.io.IOException {
        copyFile(destination, source, false);
    }

    /**
     * Copies the contents of one file to another.
     * 
     * @param destination
     *            Destination file
     * @param source
     *            Source file
     * @param deleteSourceFile
     *            whether or not to delete the source file
     * 
     * @exception java.io.IOException
     *                Thrown if there is an error copying the file.
     */
    public static void copyFile(File destination, File source,
            boolean deleteSourceFile) throws java.io.IOException {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(destination);

            int len;
            byte[] buffer = new byte[4096];
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
        } catch (java.io.IOException e) {
            throw e;
        } finally {
            in.close();
            out.close();
        }

        if (deleteSourceFile) {
            source.delete();
        }
    }
}

Related

  1. copyFile(File base, String relfilepath, File targetdir)
  2. copyFile(File copyFrom, File copyTo)
  3. copyFile(File copyFrom, File copyTo)
  4. copyFile(File destfile, File srcfile)
  5. copyFile(File destFile, File srcFile)
  6. copyFile(File existingFile, File destFile)
  7. copyFile(File file, File destPath)
  8. copyFile(File file, File dir)
  9. copyFile(File file, File newFile, boolean overwrite, boolean setLastModified)