Here you can find the source of copyDirectory(File src, File dest)
Parameter | Description |
---|---|
src | The directory to copy from. |
dest | The directory to be created. |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyDirectory(File src, File dest) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2013 Nick Robinson 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 ******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { /**//from www . j a v a 2 s . co m * Copies a directory recursively. * * @param src * The directory to copy from. * @param dest * The directory to be created. * @throws IOException */ public static void copyDirectory(File src, File dest) throws IOException { dest.mkdir(); for (File file : src.listFiles()) { if (file.isDirectory()) { copyDirectory(file, new File(dest, file.getName())); } else { copyFile(file, new File(dest, file.getName())); } } } /** * Copies a single file. * * @param sourceFile * @param destFile * @throws IOException */ 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(); } } } }