Java Copy Directory copyDirectory(final File sourceDir, final File destDir)

Here you can find the source of copyDirectory(final File sourceDir, final File destDir)

Description

copy Directory

License

Open Source License

Parameter

Parameter Description
src source.
dest destination.

Declaration

public static void copyDirectory(final File sourceDir, final File destDir) throws IOException 

Method Source Code

//package com.java2s;
/**/* w  w w.j ava  2s . c  om*/
 * Copyright (c) {2011} {meter@rbtsb.com} {
 * individual contributors as indicated by the @authors tag}.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Private License v1.0
 * which accompanies this distribution, and is available at
 * http://www.rbtsb.com
 */

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

public class Main {
    /**
     * @param src
     *            source.
     * @param dest
     *            destination.
     */
    public static void copyDirectory(final File sourceDir, final File destDir) throws IOException {
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        File[] children = sourceDir.listFiles();
        for (File sourceChild : children) {
            String name = sourceChild.getName();
            File destChild = new File(destDir, name);
            if (sourceChild.isDirectory()) {
                copyDirectory(sourceChild, destChild);

            } else {
                copyFile(sourceChild, destChild);

            }
        }
    }

    /**
     * @param source
     *            source.
     * @param dest
     *            destination.
     * @throws Exception .
     */
    public static void copyFile(final File source, final File dest) throws IOException {
        if (!dest.getParentFile().exists()) {
            String dir = dest.getParentFile().toString();
            new File(dir).mkdir();
        }
        InputStream in = null;
        OutputStream out = null;
        if (source.exists()) {
            try {
                in = new FileInputStream(source);
                out = new FileOutputStream(dest);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                if (in != null)
                    in.close();
                if (out != null)
                    out.close();
            }
        }
    }
}

Related

  1. copyDirectory(File srcDir, File dstDir)
  2. copyDirectory(File srcDir, File dstDir)
  3. copyDirectory(File srcFile, File destFile)
  4. copyDirectory(File srcPath, File dstPath)
  5. copyDirectory(File srcPath, File dstPath)
  6. copyDirectory(final File src, final File dest)
  7. copyDirectory(final String sourceDir, final String targetDir)
  8. copyDirectory(String sourceDir, String targetDir)
  9. copyDirectory(String srcDir, String destDir)