Java Directory Copy nio copyDirectoryRecursively(File source, File target)

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

Description

copy Directory Recursively

License

Apache License

Declaration

private static void copyDirectoryRecursively(File source, File target) throws IOException 

Method Source Code


//package com.java2s;
/*-//ww w  .  j  av a 2  s . co m
 * ========================LICENSE_START=================================
 * com.geewhiz.pacify.common
 * %%
 * Copyright (C) 2011 - 2017 Sven Oppermann
 * %%
 * 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.
 * =========================LICENSE_END==================================
 */

import java.io.File;

import java.io.IOException;

import java.nio.file.Files;

public class Main {
    private static void copyDirectoryRecursively(File source, File target) throws IOException {
        if (!target.exists()) {
            Files.copy(source.toPath(), target.toPath(), java.nio.file.StandardCopyOption.COPY_ATTRIBUTES);
        }

        for (String child : source.list()) {
            copyDirectory(new File(source, child), new File(target, child));
        }
    }

    public static void copyDirectory(File sourceDir, File targetDir) throws IOException {
        if (sourceDir.isDirectory()) {
            copyDirectoryRecursively(sourceDir.getAbsoluteFile(), targetDir.getAbsoluteFile());
        } else {
            Files.copy(sourceDir.getAbsoluteFile().toPath(), targetDir.getAbsoluteFile().toPath(),
                    java.nio.file.StandardCopyOption.COPY_ATTRIBUTES);
        }
    }
}

Related

  1. copyDirectory(final Path source, final Path destination)
  2. copyDirectory(final Path source, final Path destination, List excludes)
  3. copyDirectory(Path source, ArrayList targets)
  4. copyDirectory(String fromPath, String toPath)
  5. copyDirectory(String source, String destination)
  6. copyDirectoryToDirectory(File srcDir, File destDir)
  7. copyDirNIO(File sourceLocation, File targetLocation)