Here you can find the source of copyDirectory(File in, File out)
public static void copyDirectory(File in, File out) throws IOException
//package com.java2s; /*//from w ww . j a v a2 s .com * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2011 OpenConcerto, 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 { private static final int CHANNEL_MAX_COUNT = Math.min(64 * 1024 * 1024 - 32 * 1024, Integer.MAX_VALUE); 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 { copyDirectory(in, out, toIgnore, false); } public static void copyDirectory(File in, File out, final Set<String> toIgnore, final boolean useTime) 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, useTime); } } else { if (!in.getName().equals("Thumbs.db")) { copyFile(in, out, useTime); } } } 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) { return list(root, depth, depth, ff); } public static final List<File> list(final File root, final int minDepth, final int maxDepth, final FileFilter ff) { return list(root, minDepth, maxDepth, ff, false); } public static final List<File> list(final File root, final int minDepth, final int maxDepth, final FileFilter ff, final boolean sort) { if (minDepth > maxDepth) throw new IllegalArgumentException(minDepth + " > " + maxDepth); if (maxDepth < 0) throw new IllegalArgumentException(maxDepth + " < 0"); if (!root.exists()) return Collections.<File>emptyList(); final File currentFile = accept(ff, minDepth, maxDepth, root, 0) ? root : null; if (maxDepth == 0) { return currentFile == null ? Collections.<File>emptyList() : Collections.singletonList(currentFile); } else { final List<File> res = new ArrayList<File>(); final File[] children = root.listFiles(); if (children == null) throw new IllegalStateException("cannot list " + root); if (sort) Arrays.sort(children); for (final File child : children) { if (maxDepth > 1 && child.isDirectory()) { res.addAll(list(child, minDepth - 1, maxDepth - 1, ff, sort)); } else if (accept(ff, minDepth, maxDepth, child, 1)) { res.add(child); } } if (currentFile != null) res.add(currentFile); return res; } } public static void copyFile(File in, File out) throws IOException { copyFile(in, out, CHANNEL_MAX_COUNT); } /** * Copy a file. It is generally not advised to use 0 for <code>maxCount</code> since various * implementations have size limitations, see {@link #copyFile(File, File)}. * * @param in the source file. * @param out the destination file. * @param maxCount the number of bytes to copy at a time, 0 meaning size of <code>in</code>. * @throws IOException if an error occurs. */ public static void copyFile(File in, File out, long maxCount) throws IOException { final FileInputStream sourceIn = new FileInputStream(in); FileOutputStream sourceOut = null; try { sourceOut = new FileOutputStream(out); final FileChannel sourceChannel = sourceIn.getChannel(); final long size = sourceChannel.size(); if (maxCount == 0) maxCount = size; final FileChannel destinationChannel = sourceOut.getChannel(); long position = 0; while (position < size) { position += sourceChannel.transferTo(position, maxCount, destinationChannel); } } finally { sourceIn.close(); if (sourceOut != null) sourceOut.close(); } } public static void copyFile(File in, File out, final boolean useTime) throws IOException { if (!useTime || in.lastModified() != out.lastModified()) { copyFile(in, out); if (useTime) out.setLastModified(in.lastModified()); } } private static final boolean accept(final FileFilter ff, final int minDepth, final int maxDepth, final File f, final int depth) { return minDepth <= depth && depth <= maxDepth && (ff == null || ff.accept(f)); } }