Here you can find the source of copyRecursively(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
public static void copyRecursively(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
//package com.java2s; /*// w ww . j ava2s . co 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.LinkOption; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileAttribute; import java.util.concurrent.atomic.AtomicLong; public class Main { public static void copyRecursively(Path source, Path target, CopyOption... copyOptions) { copyRecursively(source, target, null, copyOptions); } public static void copyRecursively(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions) { try { if (!Files.exists(source, LinkOption.NOFOLLOW_LINKS)) { throw new RuntimeException("Source path does not exist " + source.toAbsolutePath().toString()); } if (!Files.isDirectory(source)) { copyRecursivelyHelper(source, target, bytesCounter, copyOptions); } 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()); } // Create target parent directory if it does not exist Files.createDirectories(target.getParent()); copyRecursivelyHelper(source, target, bytesCounter, copyOptions); } } catch (IOException ex) { throw new RuntimeException("Failed to copy " + source.toAbsolutePath().toString() + " to " + target.toAbsolutePath().toString(), ex); } } /** * 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); } } public static void createDirectories(Path path, FileAttribute<?>... attrs) { try { Files.createDirectories(path, attrs); } catch (IOException ex) { throw new RuntimeException("Failed to create directory " + path.toAbsolutePath().toString(), ex); } } }