Java Directory Copy nio copyDirectory(File source, File target, boolean acceptFileInUseError)

Here you can find the source of copyDirectory(File source, File target, boolean acceptFileInUseError)

Description

copy Directory

License

Open Source License

Declaration

public static void copyDirectory(File source, File target,
            boolean acceptFileInUseError) 

Method Source Code

//package com.java2s;
/* MOD_V2.0//from  ww  w  .  j  ava  2 s.  c o  m
 * Copyright (c) 2012 OpenDA Association
 * All rights reserved.
 * 
 * This file is part of OpenDA. 
 * 
 * OpenDA 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. 
 * 
 * OpenDA 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 OpenDA.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Copy file contents from source to destination
     * using a loop with chunks smaller than 64Mb - 32Kb,
     * otherwise on Windows, especially when copying to UNC paths,
     * with files bigger than 64MB, a "java.io.IOException:
     * Insufficient system resources exist to complete the requested service" could be thrown.
     * see http://forums.oracle.com/forums/thread.jspa?messageID=8622867
     * Also, without using small chunks, on some windows systems
     * for files larger than about 260 MB could get the error
     * "java.io.IOException: Map failed".
     * See http://www.coderanch.com/t/279109/Streams/java/Java-nio
     */
    private static final int MAX_COUNT = (64 * 1024 * 1024) - (32 * 1024);

    public static void copyDirectory(File source, File target,
            boolean acceptFileInUseError) {
        if (source.getName().equalsIgnoreCase(".svn")) {
            return;
        }
        if (!target.exists()) {
            boolean success = target.mkdirs();
            if (!success) {
                throw new RuntimeException(
                        "BBUtils.copyDirectory: could not create directory "
                                + target.getAbsolutePath());
            }
        }
        String[] files = source.list();

        for (String file : files) {
            File src = new File(source, file);
            File tgt = new File(target, file);

            if (src.isDirectory()) {
                copyDirectory(src, tgt, acceptFileInUseError);
            } else {
                try {
                    copyFile(src, tgt);
                } catch (IOException e) {
                    if (acceptFileInUseError
                            && e.getMessage()
                                    .contains(
                                            "The process cannot access the file because it is being used by another process")) {
                        // no action
                    } else {
                        throw new RuntimeException(
                                "BBUtils.copyDirectory: could not copy file "
                                        + src.getAbsolutePath() + " to "
                                        + tgt.getAbsolutePath() + ":"
                                        + e.getMessage(), e);
                    }
                }
            }
        }

        copyFilePermissions(source, target);
    }

    public static void copyFile(File source, File target)
            throws IOException {
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            inChannel = new FileInputStream(source).getChannel();
            outChannel = new FileOutputStream(target).getChannel();

            //Copy file contents from source to destination
            //Use a loop with chunks smaller than 64Mb - 32Kb,
            //otherwise on Windows, especially when copying to UNC paths,
            //with files bigger than 64MB, a "java.io.IOException: 
            //Insufficient system resources exist to complete the requested service" could be thrown.
            //see http://forums.oracle.com/forums/thread.jspa?messageID=8622867
            //Also, without using small chunks, on some windows systems
            //for files larger than about 260 MB could get the error
            //"java.io.IOException: Map failed".
            //See http://www.coderanch.com/t/279109/Streams/java/Java-nio
            long fileSize = inChannel.size();
            long position = 0;
            while (position < fileSize) {
                position += inChannel.transferTo(position, MAX_COUNT,
                        outChannel);
            }
        } finally {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }

        copyFilePermissions(source, target);
    }

    /**
     * Sets the file permissions of the given source file
     * on the given target file.
     *
     * @param source
     * @param target
     */
    private static void copyFilePermissions(File source, File target) {
        if (source.canRead()) {
            target.setReadable(true, false);
        } else {
            target.setReadable(false, false);
        }

        if (source.canWrite()) {
            target.setWritable(true, false);
        } else {
            target.setWritable(false, false);
        }

        if (source.canExecute()) {
            target.setExecutable(true, false);
        } else {
            target.setExecutable(false, false);
        }
    }
}

Related

  1. copyDirectory(File source, File dest, CopyOption... options)
  2. copyDirectory(File source, File destination)
  3. copyDirectory(File source, File destination)
  4. copyDirectory(File source, File destination)
  5. copyDirectory(File source, File destination, boolean recursive)
  6. copyDirectory(File sourceDir, File destinationDir)
  7. copyDirectory(File sourceDir, File targetDir)
  8. copyDirectory(File sourceDirectory, File targetDirectory)
  9. copyDirectory(File sourceFile, File destFile)