Java Directory Copy nio copyDirectory(File src, File dest)

Here you can find the source of copyDirectory(File src, File dest)

Description

Copies a directory recursively.

License

Open Source License

Parameter

Parameter Description
src The directory to copy from.
dest The directory to be created.

Exception

Parameter Description
IOException an exception

Declaration

public static void copyDirectory(File src, File dest) throws IOException 

Method Source Code

//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();
            }
        }
    }
}

Related

  1. copyDirectory(File source, File target, boolean acceptFileInUseError)
  2. copyDirectory(File sourceDir, File destinationDir)
  3. copyDirectory(File sourceDir, File targetDir)
  4. copyDirectory(File sourceDirectory, File targetDirectory)
  5. copyDirectory(File sourceFile, File destFile)
  6. copyDirectory(File src, File target)
  7. copyDirectory(File srcDir, File destDir)
  8. copyDirectory(File srcDir, File destDir, boolean preserveFileDate)
  9. copyDirectory(final File source, final File destination)