Java Copy Directory copyDirectory(final File src, final File dest)

Here you can find the source of copyDirectory(final File src, final File dest)

Description

copy Directory

License

Open Source License

Declaration

public static void copyDirectory(final File src, final File dest) throws IOException 

Method Source Code


//package com.java2s;
/*//from w ww  .  j  a v a2 s. co  m
 * JBoss, Home of Professional Open Source.
 * Copyright 2012 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
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(final File src, final File dest) throws IOException {
        String[] files = src.list();
        for (String f : files) {
            File current = new File(src, f);
            if (current.isDirectory()) {
                copyDirectory(current, new File(dest, f));
            } else {
                copyFile(current, new File(dest, f));
            }
        }
    }

    public static void copyFile(final File src, final File dest) throws IOException {
        final InputStream in = new BufferedInputStream(new FileInputStream(src));
        try {
            copyFile(in, dest);
        } finally {
            close(in);
        }
    }

    public static void copyFile(final InputStream in, final File dest) throws IOException {
        dest.getParentFile().mkdirs();
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
        try {
            int i = in.read();
            while (i != -1) {
                out.write(i);
                i = in.read();
            }
        } finally {
            close(out);
        }
    }

    public static void close(Closeable closeable) {
        try {
            closeable.close();
        } catch (IOException ignore) {
        }
    }
}

Related

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