Java FileChannel Copy copyFile(File source, File destination)

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

Description

Copies a file.

License

Open Source License

Parameter

Parameter Description
source the name of the source file
destination the name of the destination file

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(File source, File destination) 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.nio.channels.FileChannel;

public class Main {
    /**//from   w  w w  .j a v  a 2 s . c  om
     * Copies a file.
     * 
     * @param source
     *            the name of the source file
     * @param destination
     *            the name of the destination file
     * @throws IOException
     */
    public static void copyFile(File source, File destination) throws IOException {
        FileChannel srcChannel = new FileInputStream(source).getChannel();
        FileChannel dstChannel = new FileOutputStream(destination).getChannel();

        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

        srcChannel.close();
        dstChannel.close();
    }

    public static void copyFile(String source, String destination) throws IOException {
        copyFile(new File(source), new File(destination));
    }
}

Related

  1. copyFile(File source, File destination)
  2. copyFile(File source, File destination)
  3. copyFile(File source, File destination)
  4. copyFile(File source, File destination)
  5. copyFile(File source, File destination)
  6. copyFile(File source, File destination, boolean overwrite)
  7. copyFile(File source, File destination, long chunkSize, boolean overwrite)
  8. copyFile(File source, File target)
  9. copyFile(File source, File target)