Java FileChannel Copy copyFile(String srcPath, String destPath)

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

Description

copy File

License

Open Source License

Return

true if copy succeed.

Declaration

public static boolean copyFile(String srcPath, String destPath) 

Method Source Code


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

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

public class Main {
    /**/*from   w  ww. j a va  2  s. c  om*/
     * @return true if copy succeed.
     */
    public static boolean copyFile(String srcPath, String destPath) {
        boolean success = false;
        FileChannel srcChannel = null;
        FileChannel destChannel = null;

        try {
            File dest = new File(destPath);
            if (dest.exists())
                dest.createNewFile();
            srcChannel = new FileInputStream(srcPath).getChannel();
            destChannel = new FileOutputStream(destPath).getChannel();

            srcChannel.transferTo(0, srcChannel.size(), destChannel);
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
            success = false;
        } finally {
            try {
                if (srcChannel != null)
                    srcChannel.close();
                if (destChannel != null)
                    destChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return success;
    }
}

Related

  1. copyFile(String sourceFile, String destFile)
  2. copyFile(String sourceFilename, String destFilename)
  3. copyFile(String sourceFileName, String destinationFileName)
  4. copyFile(String src, String dest)
  5. copyFile(String srcFileName, String desFileName)
  6. copyFileByChannel(String srcFileName, String dstFileName)
  7. copyFileContent(final String sourcePath, final String targetPath)
  8. copyFileIntoProjectFolder(String projectName, File file)
  9. copyFileLocking(File copyFrom, File copyTo)