Here you can find the source of copyDir(File dir1, File dir2)
public static void copyDir(File dir1, File dir2) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 Gabriel Skantze.// ww w. j a v a 2s . c o m * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html * * Contributors: * Gabriel Skantze - initial API and implementation ******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; import java.util.List; public class Main { public static void copyDir(File dir1, File dir2) throws IOException { if (!dir1.isDirectory()) throw new IOException("Not a directory: " + dir1); if (dir2.exists()) throw new IOException("Cannot copy to existing directory: " + dir2); dir2.mkdirs(); File[] files = dir1.listFiles(); if (files != null) { for (File f : files) { if (f.isDirectory()) { copyDir(f, new File(dir2, f.getName())); } else { copyFile(f, new File(dir2, f.getName())); } } } } public static void listFiles(String directoryName, List<String> files, String pattern) { File directory = new File(directoryName); // get all the files from a directory File[] fList = directory.listFiles(); for (File file : fList) { if (file.isFile()) { if (file.getName().matches(pattern)) files.add(file.getAbsolutePath()); } else if (file.isDirectory()) { listFiles(file.getAbsolutePath(), files, pattern); } } } public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } } }