Java Path Copy nio copyRecursivelyHelper(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)

Here you can find the source of copyRecursivelyHelper(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)

Description

Assumes that 1) source exists 2) target parent exists

License

Open Source License

Declaration

private static void copyRecursivelyHelper(Path source, Path target, AtomicLong bytesCounter,
        CopyOption... copyOptions) 

Method Source Code

//package com.java2s;
/*//from  w  ww  . j a  v a 2  s. c o m
 *  Copyright (c) 2008 - Tomas Janecek.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.io.IOException;

import java.nio.file.CopyOption;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.concurrent.atomic.AtomicLong;

public class Main {
    /**
     * Assumes that
     * 1) source exists
     * 2) target parent exists
     */
    private static void copyRecursivelyHelper(Path source, Path target, AtomicLong bytesCounter,
            CopyOption... copyOptions) {
        try {
            if (!Files.isDirectory(source)) {
                Files.copy(source, target, copyOptions);
                if (bytesCounter != null) {
                    BasicFileAttributes attributes = Files.readAttributes(source, BasicFileAttributes.class);
                    bytesCounter.addAndGet(attributes.size());
                }

            } else {
                if (Files.exists(target) && !Files.isDirectory(target)) {
                    throw new RuntimeException("Source is a directory " + source.toAbsolutePath().toString()
                            + " target exists but is not a directory " + target.toAbsolutePath().toString());
                }

                // Copy directory itself
                Files.copy(source, target, copyOptions);

                // Copy directory content recursively
                for (Path sourceChild : Files.newDirectoryStream(source)) {
                    Path targetChild = target.resolve(sourceChild.getFileName());
                    copyRecursivelyHelper(sourceChild, targetChild, bytesCounter, copyOptions);
                }
            }
        } catch (IOException ex) {
            throw new RuntimeException("Failed to copy " + source.toAbsolutePath().toString() + " to "
                    + target.toAbsolutePath().toString(), ex);
        }
    }
}

Related

  1. copyRecursively(File fromDirectory, File toDirectory)
  2. copyRecursively(File src, File dest)
  3. copyRecursively(File src, File dest)
  4. copyRecursively(final Path source, final Path destination, final CopyOption... options)
  5. copyRecursively(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
  6. copyResource(String resourceLocation, Path target)
  7. copyResource(String source, Path target, Class cls)
  8. copyResources(final URL source, final Path destination)
  9. copyToDir(Path source, Path targetDir, CopyOption... options)