Java Directory Copy copyDirectory(File source, File target)

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

Description

Copy the given source directory (and all its contents) to the given target directory.

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Pivotal Software, Inc.
 * 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:/*from  w  w w.  jav  a  2s . com*/
 *     Pivotal Software, Inc. - initial API and implementation
 *******************************************************************************/

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 {
    /**
     * Copy the given source directory (and all its contents) to the given
     * target directory.
     */
    public static void copyDirectory(File source, File target) throws IOException {
        if (!target.exists()) {
            target.mkdirs();
        }
        File[] files = source.listFiles();
        if (files == null) {
            return;
        }
        for (File sourceChild : files) {
            String name = sourceChild.getName();
            if (name.equals(".svn")) {
                continue;
            }
            File targetChild = new File(target, name);
            if (sourceChild.isDirectory()) {
                copyDirectory(sourceChild, targetChild);
            } else {
                copy(sourceChild, targetChild);
            }
        }
    }

    /**
     * Copy file from src (path to the original file) to dest (path to the
     * destination file).
     */
    private static void copy(File src, File dest) throws IOException {
        InputStream in = new FileInputStream(src);
        try {
            OutputStream out = new FileOutputStream(dest);
            try {
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }
}

Related

  1. copyDirectory(File source, File destination)
  2. copyDirectory(File source, File destination)
  3. copyDirectory(File source, File destination, boolean overwrite)
  4. copyDirectory(File source, File destination, String endWith)
  5. copyDirectory(File source, File target)
  6. copyDirectory(File source, File target)
  7. copyDirectory(File source, File target)
  8. copyDirectory(File source, File target)
  9. copyDirectory(File source, File target)