Java Directory Copy nio copyDirectory(File srcDir, File destDir)

Here you can find the source of copyDirectory(File srcDir, File destDir)

Description

copy Directory

License

Open Source License

Declaration

public static void copyDirectory(File srcDir, File destDir) throws IOException 

Method Source Code


//package com.java2s;
/*//www . j  a  va  2s.  co  m
 * Hello Minecraft!.
 * Copyright (C) 2013  huangyuhui <huanghongxun2008@126.com>
 *
 * 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 3 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, see {http://www.gnu.org/licenses/}.
 */

import java.io.File;
import java.io.FileFilter;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void copyDirectory(File srcDir, File destDir) throws IOException {
        copyDirectory(srcDir, destDir, null);
    }

    public static void copyDirectory(File srcDir, File destDir, FileFilter filter) throws IOException {
        if (srcDir == null)
            throw new NullPointerException("Source must not be null");
        if (destDir == null)
            throw new NullPointerException("Destination must not be null");
        if (!srcDir.exists())
            throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
        if (!srcDir.isDirectory())
            throw new IOException("Source '" + srcDir + "' exists but is not a directory");
        if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath()))
            throw new IOException("Source '" + srcDir + "' and destination '" + destDir + "' are the same");

        List exclusionList = null;
        if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
            File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
            if ((srcFiles != null) && (srcFiles.length > 0)) {
                exclusionList = new ArrayList(srcFiles.length);
                for (File srcFile : srcFiles) {
                    File copiedFile = new File(destDir, srcFile.getName());
                    exclusionList.add(copiedFile.getCanonicalPath());
                }
            }
        }
        doCopyDirectory(srcDir, destDir, filter, exclusionList);
    }

    public static String getName(String filename) {
        if (filename == null)
            return null;
        int index = indexOfLastSeparator(filename);
        return filename.substring(index + 1);
    }

    private static void doCopyDirectory(File srcDir, File destDir, FileFilter filter, List<String> exclusionList)
            throws IOException {
        File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
        if (srcFiles == null)
            throw new IOException("Failed to list contents of " + srcDir);
        if (destDir.exists()) {
            if (!destDir.isDirectory())
                throw new IOException("Destination '" + destDir + "' exists but is not a directory");
        } else if ((!destDir.mkdirs()) && (!destDir.isDirectory()))
            throw new IOException("Destination '" + destDir + "' directory cannot be created");

        if (!destDir.canWrite())
            throw new IOException("Destination '" + destDir + "' cannot be written to");
        for (File srcFile : srcFiles) {
            File dstFile = new File(destDir, srcFile.getName());
            if ((exclusionList == null) || (!exclusionList.contains(srcFile.getCanonicalPath())))
                if (srcFile.isDirectory())
                    doCopyDirectory(srcFile, dstFile, filter, exclusionList);
                else
                    doCopyFile(srcFile, dstFile);
        }
        destDir.setLastModified(srcDir.lastModified());
    }

    public static int indexOfLastSeparator(String filename) {
        if (filename == null)
            return -1;
        int lastUnixPos = filename.lastIndexOf(47);
        int lastWindowsPos = filename.lastIndexOf(92);
        return Math.max(lastUnixPos, lastWindowsPos);
    }

    public static void doCopyFile(File srcFile, File destFile) throws IOException {
        Files.copy(srcFile.toPath(), destFile.toPath(), StandardCopyOption.COPY_ATTRIBUTES,
                StandardCopyOption.REPLACE_EXISTING);
    }
}

Related

  1. copyDirectory(File sourceDir, File targetDir)
  2. copyDirectory(File sourceDirectory, File targetDirectory)
  3. copyDirectory(File sourceFile, File destFile)
  4. copyDirectory(File src, File dest)
  5. copyDirectory(File src, File target)
  6. copyDirectory(File srcDir, File destDir, boolean preserveFileDate)
  7. copyDirectory(final File source, final File destination)
  8. copyDirectory(final File sourceFile, final File targetDir)
  9. copyDirectory(final File srcDir, final File destDir)