Here you can find the source of copyDirectory(File source, File destination)
Parameter | Description |
---|---|
source | directory |
destination | directory |
Parameter | Description |
---|---|
IOException | on a IO problem during copy |
public static void copyDirectory(File source, File destination) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2008-2011 Chair for Applied Software Engineering, * Technische Universitaet Muenchen.// w w w . ja va 2s . c o m * All rights reserved. This program and the accompanying materials * are 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: ******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class Main { /** * Copy a directory from source to target including its contained files and directories. * * @param source directory * @param destination directory * @throws IOException on a IO problem during copy */ public static void copyDirectory(File source, File destination) throws IOException { destination.mkdirs(); if (!source.exists()) { return; } for (File file : source.listFiles()) { if (file.isDirectory()) { copyDirectory(file, new File(destination.getAbsolutePath() + File.separatorChar + file.getName())); } else { copyFile(file, new File(destination.getAbsolutePath() + File.separatorChar + file.getName())); } } } /** * This method copies a single file. * * @param source the source * @param destination the destination * @throws IOException copy problem */ public static void copyFile(File source, File destination) throws IOException { copyFile(new FileInputStream(source), destination); } /** * This method copies a single file. * * @param source the source input stream * @param destination the destination * @throws IOException copy problem */ public static void copyFile(InputStream source, File destination) throws IOException { if (source == null || destination == null) { throw new IOException("Source or destination is null."); } if (destination.getParentFile() != null) { destination.getParentFile().mkdirs(); } FileOutputStream outputStream = new FileOutputStream(destination); byte[] buffer = new byte[4096]; int read; while ((read = source.read(buffer)) != -1) { outputStream.write(buffer, 0, read); } source.close(); outputStream.close(); } }