Java Copy Directory copyDirectory(File srcDir, File destDir)

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

Description

copy Directory

License

Open Source License

Declaration

public static void copyDirectory(File srcDir, File destDir) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:/*from w w w  .  j a  va  2  s .c o m*/
 *     Jochen Hiller
 *******************************************************************************/

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 copyDirectory(File srcDir, File destDir) throws IOException {
        File[] srcFiles = srcDir.listFiles();
        if (srcFiles == null) {
            throw new IOException("Failed to list contents of " + srcDir);
        }
        if (destDir.exists()) {
            if (!(destDir.isDirectory())) {
                throw new IOException("Destination '" + destDir + "' exists but is not a directory");
            }
        } else if ((!(destDir.mkdirs())) && (!(destDir.isDirectory()))) {
            throw new IOException("Destination '" + destDir + "' directory cannot be created");
        }
        if (!(destDir.canWrite())) {
            throw new IOException("Destination '" + destDir + "' cannot be written to");
        }

        for (File srcFile : srcFiles) {
            File dstFile = new File(destDir, srcFile.getName());
            if (srcFile.isDirectory())
                copyDirectory(srcFile, dstFile);
            else {
                copyFile(srcFile, dstFile);
            }
        }
    }

    public static void copyFile(File srcFile, File destFile) throws IOException {
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        copyStream(fis, fos);
        fos.close();
        fis.close();
    }

    public static void copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
        byte[] bytes = new byte[4096];
        int read = inputStream.read(bytes, 0, 4096);
        while (read > 0) {
            outputStream.write(bytes, 0, read);
            read = inputStream.read(bytes, 0, 4096);
        }
    }
}

Related

  1. copyDirectory(File src, File dest)
  2. copyDirectory(File src, File dest, boolean force)
  3. copyDirectory(File src, File dst)
  4. copyDirectory(File srcDir, File dstDir)
  5. copyDirectory(File srcDir, File dstDir)
  6. copyDirectory(File srcDir, File dstDir)
  7. copyDirectory(File srcDir, File dstDir)