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

Internal copy file method.

License

Open Source License

Parameter

Parameter Description
srcFile the validated source file, must not be <code>null</code>
destFile the validated destination file, must not be <code>null</code>
preserveFileDate whether to preserve the file date

Exception

Parameter Description
IOException if an error occurs

Declaration

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

Method Source Code

//package com.java2s;
/*/*from ww  w.ja v  a  2  s  .  c  o m*/
 * $Id$
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

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

import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.channels.Channel;
import java.nio.channels.FileChannel;

public class Main {
    /**
     * Internal copy file method.
     * 
     * @param srcFile the validated source file, must not be <code>null</code>
     * @param destFile the validated destination file, must not be <code>null</code>
     * @param preserveFileDate whether to preserve the file date
     * @throws IOException if an error occurs
     */
    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");
        }

        FileChannel input = new FileInputStream(srcFile).getChannel();
        try {
            FileChannel output = new FileOutputStream(destFile).getChannel();
            try {
                output.transferFrom(input, 0, input.size());
            } finally {
                closeQuietly(output);
            }
        } finally {
            closeQuietly(input);
        }

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

    /**
     * Unconditionally close a <code>Channel</code>.
     * <p>
     * Equivalent to {@link Channel#close()}, except any exceptions will be ignored.
     * This is typically used in finally blocks.
     * 
     * @param channel the Channel to close, may be null or already closed
     */
    public static void closeQuietly(Channel channel) {
        try {
            if (channel != null) {
                channel.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }
}

Related

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