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

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException 

Method Source Code

//package com.java2s;
/**/*from w  ww. j  a v  a 2s . c  o  m*/
 * This file is part of VisiCut.
 * Copyright (C) 2011 - 2013 Thomas Oster <thomas.oster@rwth-aachen.de>
 * RWTH Aachen University - 52062 Aachen, Germany
 *
 *     VisiCut is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     VisiCut 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 Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with VisiCut.  If not, see <http://www.gnu.org/licenses/>.
 **/

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 int FILE_COPY_BUFFER_SIZE = 1024 * 1024 * 30;

    public static void copyFile(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 > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : 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. copyFile(File src, File dst)
  2. copyFile(File src, File target)
  3. copyFile(File src, File target)
  4. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  5. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  6. copyFile(File srcFile, File dstFile, boolean overwrite)
  7. copyFile(FileChannel in, FileChannel out)
  8. copyFile(FileInputStream fromFile, FileOutputStream toFile)
  9. copyFile(final File in, final File out)