Java File Copy nio copy(File sourceFile, File destFile)

Here you can find the source of copy(File sourceFile, File destFile)

Description

Copy a source file to a specified destination path.

License

Open Source License

Parameter

Parameter Description
sourceFile the source
destFile the destination (will be created if doesn't exist, will be overwritten if it exists)

Exception

Parameter Description
IOException we never know...

Declaration

public static void copy(File sourceFile, File destFile) 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  ww. j a v a 2 s.  c o m
     * Copy a source file to a specified destination path.
     * @param sourceFile the source
     * @param destFile the destination (will be created if doesn't exist, will
     * be overwritten if it exists)
     * @throws IOException we never know...
     */
    public static void copy(File sourceFile, File destFile) throws IOException {

        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }

    }
}

Related

  1. copy(File source, File destination)
  2. copy(File source, File target)
  3. copy(File source, File target)
  4. copy(File source, File target)
  5. copy(File source, File target, FilenameFilter filter)
  6. copy(File sourceFile, File destFile)
  7. copy(File sourceFile, File destinationFile)
  8. copy(File sourceFile, File targetFile)
  9. copy(File src, File dest)