Here you can find the source of copyDirectory(File sourceLocation, File targetLocation)
public static void copyDirectory(File sourceLocation, File targetLocation) throws IOException
//package com.java2s; /* /*from ww w. j a va 2 s .com*/ * Siberia utilities : siberia plugin providing severall utilities classes * * Copyright (C) 2008 Alexis PARIS * Project Lead: Alexis Paris * * This library 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 2.1 of the License, or (at your option) * any later version. * * This library 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 this library; * if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void copyDirectory(File sourceLocation, File targetLocation) throws IOException { copyDirectory(sourceLocation, targetLocation, null); } public static void copyDirectory(File sourceLocation, File targetLocation, FileFilter filter) throws IOException { if (sourceLocation != null) { if (sourceLocation.exists()) { if (sourceLocation.isDirectory()) { if (!targetLocation.exists()) { targetLocation.mkdirs(); } File[] children = sourceLocation.listFiles(filter); for (int i = 0; i < children.length; i++) { copyDirectory(children[i], new File(targetLocation, children[i].getName()), null); } } else { if (!targetLocation.exists()) targetLocation.createNewFile(); InputStream in = new FileInputStream(sourceLocation); OutputStream out = new FileOutputStream(targetLocation); // Copy the bits from instream to outstream byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } } } } }