Java Copy Directory copyDirectoryContents(File srcDir, File dstDir)

Here you can find the source of copyDirectoryContents(File srcDir, File dstDir)

Description

copy Directory Contents

License

Apache License

Declaration

public static void copyDirectoryContents(File srcDir, File dstDir) throws IOException 

Method Source Code

//package com.java2s;
/*//w w  w .j  a v a 2  s  .  c om
 * Copyright 2005,2006 WSO2, Inc. http://www.wso2.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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 {
    public static void copyDirectoryContents(File srcDir, File dstDir) throws IOException {
        File[] listFiles = srcDir.listFiles();
        if (listFiles != null) {
            for (File srcFile : listFiles) {
                if (srcFile.isDirectory()) {
                    copyDirectory(srcFile, new File(dstDir, srcFile.getName()));
                } else {
                    copy(srcFile, new File(dstDir, srcFile.getName()));
                }
            }
        }
    }

    public static void copyDirectory(File srcDir, File dstDir) throws IOException {
        if (srcDir.isDirectory()) {
            if (!dstDir.exists()) {
                dstDir.mkdirs();
            }

            String[] children = srcDir.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(srcDir, children[i]), new File(dstDir, children[i]));
            }
        } else {
            copy(srcDir, dstDir);
        }
    }

    public static void copy(File src, File dst) throws IOException {
        if (dst.getParentFile() != null && !dst.getParentFile().exists()) {
            dst.getParentFile().mkdirs();
        }
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
}

Related

  1. copyDirectoryAndOverwriteFilesIfNeeded(File srcDir, File destDir)
  2. copyDirectoryContent(File srcPath, File targetPath)
  3. copyDirectoryContents(File directory, String targetDirName, boolean update)
  4. copyDirectoryContents(File source, File destination)
  5. copyDirectoryContents(File src, File target)
  6. copyDirectoryFromJar(String jarName, String srcDir, File tmpDir)
  7. copyDirectoryTree(File copyFrom, File copyTo)
  8. copyDirectoryWithContents(final File srcPath, final File dstPath)