Java Directory Copy copyDirectory(File source, File destination)

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

Description

Copy a directory from source to target including its contained files and directories.

License

Open Source License

Parameter

Parameter Description
source directory
destination directory

Exception

Parameter Description
IOException on a IO problem during copy

Declaration

public static void copyDirectory(File source, File destination) throws IOException 

Method Source Code

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

Related

  1. copyDirectory(File from, File to)
  2. copyDirectory(File inDir, File outDir)
  3. copyDirectory(File inDir, File outDir)
  4. copyDirectory(File inputFolder, File outputFolder)
  5. copyDirectory(File source, File destination)
  6. copyDirectory(File source, File destination)
  7. copyDirectory(File source, File destination)
  8. copyDirectory(File source, File destination, boolean overwrite)
  9. copyDirectory(File source, File destination, String endWith)