Here you can find the source of copyDirectory(File source, File destination, boolean overwrite)
private static void copyDirectory(File source, File destination, boolean overwrite) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2011 Red Hat, Inc.//from www . j a va2 s . c o m * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Red Hat, Inc. - initial API and implementation ******************************************************************************/ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import org.eclipse.core.runtime.Assert; public class Main { private static final byte[] buffer = new byte[1024]; private static void copyDirectory(File source, File destination, boolean overwrite) throws IOException { Assert.isLegal(source != null); Assert.isLegal(source.isDirectory()); Assert.isLegal(destination != null); destination = getDestinationDirectory(source, destination); if (!destination.exists()) { destination.mkdir(); } for (File content : source.listFiles()) { if (content.isDirectory()) { copyDirectory(content, new File(destination, content.getName()), overwrite); } else { copyFile(content, new File(destination, content.getName()), overwrite); } } } public static boolean isDirectory(File file) { return file != null && file.isDirectory(); } private static File getDestinationDirectory(File source, File destination) { if (!source.getName().equals(destination.getName())) { destination = new File(destination, source.getName()); } return destination; } public static boolean exists(File file) { return file != null && file.exists(); } private static void copyFile(File source, File destination, boolean overwrite) throws IOException { Assert.isLegal(source != null); Assert.isLegal(source.isFile()); Assert.isLegal(destination != null); destination = getDestinationFile(source, destination); if (exists(destination) && !overwrite) { return; } if (isDirectory(destination)) { if (!overwrite) { return; } destination.delete(); } writeTo(source, destination); } private static File getDestinationFile(File source, File destination) { if (!source.getName().equals(destination.getName())) { destination = new File(destination, source.getName()); } return destination; } private static final void writeTo(File source, File destination) throws IOException { Assert.isLegal(source != null); Assert.isLegal(destination != null); writeTo(new BufferedInputStream(new FileInputStream(source)), destination); } public static final void writeTo(String content, File destination) throws FileNotFoundException { PrintWriter writer = new PrintWriter(destination); writer.write(content); writer.flush(); writer.close(); } private static final void writeTo(InputStream in, File destination) throws IOException { Assert.isLegal(in != null); Assert.isLegal(destination != null); OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(destination)); for (int read = -1; (read = in.read(buffer)) != -1;) { out.write(buffer, 0, read); } out.flush(); } finally { silentlyClose(in); silentlyClose(out); } } private static void silentlyClose(InputStream in) { try { if (in != null) { in.close(); } } catch (IOException e) { // ignore } } private static void silentlyClose(OutputStream out) { try { if (out != null) { out.close(); } } catch (IOException e) { // ignore } } }