Java FileChannel Copy doCopyFile(File srcFile, File destFile, boolean preserveFileDate)

Here you can find the source of doCopyFile(File srcFile, File destFile, boolean preserveFileDate)

Description

From Apache Commons FileUtils

License

Apache License

Declaration

private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Geoscience Australia/*w ww.  j  a va  2s . co  m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

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

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

public class Main {
    private static final long FIFTY_MB = 1024 * 1024 * 50;

    /**
     * From Apache Commons FileUtils
     * 
     * @see http 
     *      ://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org
     *      /apache/commons/io/FileUtils.java?view=markup
     */
    private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
        if (destFile.exists() && destFile.isDirectory()) {
            throw new IOException("Destination '" + destFile + "' exists but is a directory");
        }

        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel input = null;
        FileChannel output = null;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            input = fis.getChannel();
            output = fos.getChannel();
            long size = input.size();
            long pos = 0;
            long count = 0;
            while (pos < size) {
                count = (size - pos) > FIFTY_MB ? FIFTY_MB : (size - pos);
                pos += output.transferFrom(input, pos, count);
            }
        } finally {
            output.close();
            fos.close();
            input.close();
            fis.close();
        }

        if (srcFile.length() != destFile.length()) {
            throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
        }
        if (preserveFileDate) {
            destFile.setLastModified(srcFile.lastModified());
        }
    }
}

Related

  1. copyURLToFile(URL in, File out)
  2. copyUsingNIO(String src, String dest)
  3. copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend)
  4. directoryCopy(URL source, URL destination)
  5. doCopyDirectory(File srcDir, File destDir, boolean preserveFileDate, List exclusionList)
  6. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
  7. fCopy(FileInputStream src, File dest)
  8. fileChannelCopy(File src, File dest)
  9. fileCopy(String sourceFolder, String destinationFolder)