Java Directory Copy nio copyDir(Path from, Path to, IProgressMonitor monitor)

Here you can find the source of copyDir(Path from, Path to, IProgressMonitor monitor)

Description

copy Dir

License

Apache License

Declaration

private static void copyDir(Path from, Path to, IProgressMonitor monitor) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;

import java.nio.file.SimpleFileVisitor;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.EnumSet;

import org.eclipse.core.runtime.IProgressMonitor;

public class Main {
    private static void copyDir(Path from, Path to, IProgressMonitor monitor) throws IOException {
        Files.walkFileTree(from, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new SimpleFileVisitor<Path>() {

                    @Override//from   w w w. j ava 2  s. c o  m
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                            throws IOException {
                        Path targetdir = to.resolve(from.relativize(dir));
                        try {
                            Files.copy(dir, targetdir);
                            monitor.subTask(dir.toString());
                            monitor.worked(1);
                        } catch (FileAlreadyExistsException e) {
                            if (!Files.isDirectory(targetdir))
                                throw e;
                        }
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        Files.copy(file, to.resolve(from.relativize(file)));
                        monitor.subTask(file.toString());
                        monitor.worked(1);
                        return FileVisitResult.CONTINUE;
                    }
                });
    }
}

Related

  1. copyDir(File dir1, File dir2)
  2. copyDir(File sourceDir, File targetDir)
  3. copyDir(final Path fromPath, final Path toPath)
  4. copyDir(final String src, final String dest)
  5. copyDir(String sourceDir, String targetDir)
  6. copyDir(String src, String dst)
  7. copyDirectiory(String sourceDir, String targetDir)
  8. copyDirectory(File in, File out)