Java FileChannel Copy copyFile(String source, String target)

Here you can find the source of copyFile(String source, String target)

Description

Copies source file to target.

License

Apache License

Parameter

Parameter Description
source source file to copy.
target destination to copy to.

Exception

Parameter Description
IOException when unable to copy.

Return

target as File.

Declaration

public static File copyFile(String source, String target) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.nio.channels.FileChannel;

public class Main {
    /**/* ww  w.  ja va  2s .  c  om*/
     * Copies source file to target.
     * @param source source file to copy.
     * @param target destination to copy to.
     * @return target as File.
     * @throws IOException when unable to copy.
     */
    public static File copyFile(String source, String target) throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(target).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
        } finally {
            if (inputChannel != null) {
                inputChannel.close();
            }
            if (outputChannel != null) {
                outputChannel.close();
            }
        }
        return new File(target);
    }
}

Related

  1. copyFile(String origPath, String destPath)
  2. copyFile(String source, String destination)
  3. copyFile(String source, String destination)
  4. copyFile(String source, String destination)
  5. copyFile(String source, String target)
  6. copyFile(String sourceFile, String destFile)
  7. copyFile(String sourceFilename, String destFilename)
  8. copyFile(String sourceFileName, String destinationFileName)
  9. copyFile(String src, String dest)