Java FileChannel Copy copyFile(String src, String dest)

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

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(String src, String dest) 

Method Source Code

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.nio.channels.FileChannel;

public class Main {
    public static boolean copyFile(String src, String dest) {
        return copyFile(new File(src), new File(dest));
    }/*from  w  w  w  .j a va  2  s. com*/

    public static boolean copyFile(File src, File dest) {
        boolean success = false;

        try {
            if (src.getCanonicalPath().contentEquals(dest.getCanonicalPath())) {
                System.err.println("cp: same file: " + src + " = " + dest);
            } else {
                FileChannel sourceChannel = new FileInputStream(src).getChannel();
                FileChannel destinationChannel = new FileOutputStream(dest).getChannel();
                sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
                sourceChannel.close();
                destinationChannel.close();
                success = true;
            }
        } catch (Exception e) {
            System.err.println(e);
            e.printStackTrace();
        }
        return success;
    }
}

Related

  1. copyFile(String source, String target)
  2. copyFile(String source, String target)
  3. copyFile(String sourceFile, String destFile)
  4. copyFile(String sourceFilename, String destFilename)
  5. copyFile(String sourceFileName, String destinationFileName)
  6. copyFile(String srcFileName, String desFileName)
  7. copyFile(String srcPath, String destPath)
  8. copyFileByChannel(String srcFileName, String dstFileName)
  9. copyFileContent(final String sourcePath, final String targetPath)