Java Directory Copy nio copyDirectory(Path source, ArrayList targets)

Here you can find the source of copyDirectory(Path source, ArrayList targets)

Description

copy Directory

License

Open Source License

Declaration

public static void copyDirectory(Path source, ArrayList<Path> targets) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.file.Files;
import java.nio.file.Path;

import java.nio.file.SimpleFileVisitor;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.attribute.BasicFileAttributes;
import static java.nio.file.FileVisitResult.CONTINUE;
import java.util.ArrayList;
import java.util.EnumSet;

import java.nio.file.FileAlreadyExistsException;
import java.io.IOException;

public class Main {
    public static void copyDirectory(Path source, ArrayList<Path> targets) throws IOException {

        for (Path target : targets) {
            Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                    new SimpleFileVisitor<Path>() {
                        @Override
                        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                                throws IOException {
                            Path targetdir = target.resolve(source.relativize(dir));
                            //System.out.println(targetdir); // ../tmp
                            try {
                                Files.copy(dir, targetdir);
                            } catch (FileAlreadyExistsException e) {
                                if (!Files.isDirectory(targetdir)) {
                                    throw e;
                                }/*from  ww w. ja  v  a 2s.co m*/
                            }
                            return CONTINUE;
                        }

                        @Override
                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                            Files.copy(file, target.resolve(source.relativize(file)));
                            return CONTINUE;
                        }
                    });
        }
    }
}

Related

  1. copyDirectory(final File source, final File destination)
  2. copyDirectory(final File sourceFile, final File targetDir)
  3. copyDirectory(final File srcDir, final File destDir)
  4. copyDirectory(final Path source, final Path destination)
  5. copyDirectory(final Path source, final Path destination, List excludes)
  6. copyDirectory(String fromPath, String toPath)
  7. copyDirectory(String source, String destination)
  8. copyDirectoryRecursively(File source, File target)
  9. copyDirectoryToDirectory(File srcDir, File destDir)