Java Directory Copy nio copyDir(File sourceDir, File targetDir)

Here you can find the source of copyDir(File sourceDir, File targetDir)

Description

copy Dir

License

Open Source License

Declaration

private static void copyDir(File sourceDir, File targetDir) throws IOException 

Method Source Code

//package com.java2s;
/*/*from w  ww .  j  a va  2  s.c o m*/
 * JBoss, Home of Professional Open Source.
 * Copyright 2016, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class Main {
    private static final int DEFAULT_BUFFER_SIZE = 65536;

    private static void copyDir(File sourceDir, File targetDir) throws IOException {
        if (targetDir.exists()) {
            if (!targetDir.isDirectory()) {
                throw new IOException("Not a directory " + targetDir.getAbsolutePath());
            }
        } else if (!targetDir.mkdirs()) {
            throw new IOException("Failed to create directory " + targetDir.getAbsolutePath());
        }

        File[] children = sourceDir.listFiles();
        if (children != null) {
            for (File child : children) {
                copyFile(child, new File(targetDir, child.getName()));
            }
        }
    }

    public static void copyFile(File sourceFile, File targetFile) throws IOException {
        if (sourceFile.isDirectory()) {
            copyDir(sourceFile, targetFile);
        } else {
            File parent = targetFile.getParentFile();
            if (!parent.exists()) {
                if (!parent.mkdirs()) {
                    throw new IOException("Failed to create directory " + parent.getAbsolutePath());
                }
            }
            try {
                Files.copy(sourceFile.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                throw new IOException(
                        "Failed to copy " + sourceFile.getAbsolutePath() + " to " + targetFile.getAbsolutePath(),
                        e);
            }
        }
    }

    public static void copy(final InputStream is, final File target) throws IOException {
        if (!target.getParentFile().exists()) {
            target.getParentFile().mkdirs(); // Hmm
        }
        copyStream(is, new FileOutputStream(target));
    }

    public static void copy(File source, File target) throws IOException {
        try (final FileInputStream is = new FileInputStream(source)) {
            copy(is, target);
        }
    }

    /**
     * Copy input stream to output stream without closing streams. Flushes output stream when done.
     *
     * @param is input stream
     * @param os output stream
     *
     * @throws IOException for any error
     */
    public static void copyStream(InputStream is, OutputStream os) throws IOException {
        copyStream(is, os, DEFAULT_BUFFER_SIZE);
    }

    /**
     * Copy input stream to output stream without closing streams. Flushes output stream when done.
     *
     * @param is input stream
     * @param os output stream
     * @param bufferSize the buffer size to use
     *
     * @throws IOException for any error
     */
    private static void copyStream(InputStream is, OutputStream os, int bufferSize) throws IOException {
        assert is != null : "input stream is null";
        assert os != null : "output stream is null";
        byte[] buff = new byte[bufferSize];
        int rc;
        while ((rc = is.read(buff)) != -1)
            os.write(buff, 0, rc);
        os.flush();
    }
}

Related

  1. copyDir(File dir1, File dir2)
  2. copyDir(final Path fromPath, final Path toPath)
  3. copyDir(final String src, final String dest)
  4. copyDir(Path from, Path to, IProgressMonitor monitor)
  5. copyDir(String sourceDir, String targetDir)