Java Directory Copy nio copyDirectory(File in, File out)

Here you can find the source of copyDirectory(File in, File out)

Description

copy Directory

License

Open Source License

Declaration

public static void copyDirectory(File in, File out) throws IOException 

Method Source Code

//package com.java2s;
/*//  ww  w.ja va 2s  .co  m
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2008 jOpenDocument, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU
 * General Public License Version 3 only ("GPL").  
 * You may not use this file except in compliance with the License. 
 * You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
 * See the License for the specific language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 * 
 */

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.Set;

public class Main {
    public static final FileFilter DIR_FILTER = new FileFilter() {

        public boolean accept(File f) {
            return f.isDirectory();
        }
    };

    public static void copyDirectory(File in, File out) throws IOException {
        copyDirectory(in, out, Collections.<String> emptySet());
    }

    public static void copyDirectory(File in, File out,
            final Set<String> toIgnore) throws IOException {
        if (toIgnore.contains(in.getName()))
            return;

        if (in.isDirectory()) {
            if (!out.exists()) {
                out.mkdir();
            }

            String[] children = in.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(in, children[i]), new File(out,
                        children[i]), toIgnore);
            }
        } else {
            if (!in.getName().equals("Thumbs.db")) {
                copyFile(in, out);
            }
        }
    }

    public static final List<File> list(File root, final int depth) {
        return list(root, depth, null);
    }

    /**
     * Finds all files at the specified depth below <code>root</code>.
     * 
     * @param root the base directory
     * @param depth the depth of the returned files.
     * @param ff a filter, can be <code>null</code>.
     * @return a list of files <code>depth</code> levels beneath <code>root</code>.
     */
    public static final List<File> list(File root, final int depth,
            final FileFilter ff) {
        if (!root.exists())
            return Collections.<File> emptyList();
        if (depth == 0) {
            return ff.accept(root) ? Collections.singletonList(root)
                    : Collections.<File> emptyList();
        } else if (depth == 1) {
            final File[] listFiles = root.listFiles(ff);
            if (listFiles == null)
                throw new IllegalStateException("cannot list " + root);
            return Arrays.asList(listFiles);
        } else {
            final File[] childDirs = root.listFiles(DIR_FILTER);
            if (childDirs == null)
                throw new IllegalStateException("cannot list " + root);
            final List<File> res = new ArrayList<File>();
            for (final File child : childDirs) {
                res.addAll(list(child, depth - 1, ff));
            }
            return res;
        }
    }

    public static void copyFile(File in, File out) throws IOException {
        final FileChannel sourceChannel = new FileInputStream(in)
                .getChannel();
        final FileChannel destinationChannel = new FileOutputStream(out)
                .getChannel();
        sourceChannel.transferTo(0, sourceChannel.size(),
                destinationChannel);
        sourceChannel.close();
        destinationChannel.close();
    }
}

Related

  1. copyDir(Path from, Path to, IProgressMonitor monitor)
  2. copyDir(String sourceDir, String targetDir)
  3. copyDir(String src, String dst)
  4. copyDirectiory(String sourceDir, String targetDir)
  5. copyDirectory(File in, File out)
  6. copyDirectory(File source, File dest, CopyOption... options)
  7. copyDirectory(File source, File destination)
  8. copyDirectory(File source, File destination)
  9. copyDirectory(File source, File destination)