Java FileChannel Copy copyFile(File sourceFile, File destinationFile)

Here you can find the source of copyFile(File sourceFile, File destinationFile)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File sourceFile, File destinationFile) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w w . ja va2s .  c o  m
 * Copyright (C) 2013 Jan-Olaf Becker
 * 
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class Main {
    public static void copyFile(File sourceFile, File destinationFile) throws IOException {
        copyFile(sourceFile, destinationFile, true);
    }

    public static void copyFile(File sourceFile, File destinationFile, boolean overwrite) throws IOException {
        if (sourceFile.exists()) {
            if (!destinationFile.exists()) {
                destinationFile.getParentFile().mkdirs();
                destinationFile.createNewFile();
            } else if (!overwrite)
                return;
            FileChannel sourceStream = null, destinationStream = null;
            try {
                sourceStream = new FileInputStream(sourceFile).getChannel();
                destinationStream = new FileOutputStream(destinationFile).getChannel();
                destinationStream.transferFrom(sourceStream, 0, sourceStream.size());
            } finally {
                if (sourceStream != null) {
                    sourceStream.close();
                }
                if (destinationStream != null) {
                    destinationStream.close();
                }
            }
        }
    }
}

Related

  1. copyFile(File sourceFile, File destFile)
  2. copyFile(File sourceFile, File destFile)
  3. copyFile(File sourceFile, File destFile)
  4. copyFile(File sourceFile, File destFile)
  5. copyFile(File sourceFile, File destFile)
  6. copyFile(File sourceFile, File destinationFile)
  7. copyFile(File sourceFile, File destinationFile)
  8. copyFile(File sourceFile, File targetFile)
  9. copyFile(File sourceFile, File targetFile)