Java FileChannel Copy copyFiles(File srcFile, File dstFile, boolean overwrite)

Here you can find the source of copyFiles(File srcFile, File dstFile, boolean overwrite)

Description

copy a file, allowing to overwrite if the destination already exists

License

Open Source License

Parameter

Parameter Description
srcFile the source file
dstFile the destination file
overwrite true to overwrite the destination if it exists already

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFiles(File srcFile, File dstFile, boolean overwrite) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w w  w  .j  a  v a  2s.  c  o  m*/
 * Copyright (C) 2010 Alex Cojocaru
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * copy a file, allowing to overwrite if the destination already exists
     * @param srcFile the source file
     * @param dstFile the destination file
     * @param overwrite true to overwrite the destination if it exists already
     * @throws IOException
     */
    public static void copyFiles(File srcFile, File dstFile, boolean overwrite) throws IOException {
        if (!srcFile.exists())
            throw new IOException("Source file does not exist");

        if (!overwrite && dstFile.exists())
            throw new IOException("Destination file already exists");

        FileChannel srcChannel = null, dstChannel = null;

        try {
            // Create channel on the source
            srcChannel = new FileInputStream(srcFile).getChannel();

            // Create channel on the destination
            dstChannel = new FileOutputStream(dstFile).getChannel();

            // Copy file contents from source to destination
            dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
        } finally {
            // Close the channels
            if (srcChannel != null)
                srcChannel.close();

            if (dstChannel != null)
                dstChannel.close();
        }
    }

    /**
     * @param filepath the path of the file whose existence has to be verified
     * @return true if the file denoted by filepath exists
     */
    public static boolean exists(String filepath) {
        return new File(filepath).exists();
    }
}

Related

  1. copyFileNIO(File src, File dest)
  2. copyFileNio(File src, File dst)
  3. copyFileOrDirectory(File source, File destination, boolean flag)
  4. copyFiles(File originalFile, File destinationFile)
  5. copyFiles(File source, File dest)
  6. copyFiles(final File fromFile, final File toFile)
  7. copyFileToDir(File file, File destDir)
  8. copyFileToDirectory(String fileFrom, String destinationDirectory)
  9. copyFileToFolder(final String resultFile, final String targetPath)