com.power.common.file.CopyDirVisitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-util Show documentation
Show all versions of common-util Show documentation
ApplicationPower common-util
The newest version!
package com.power.common.file;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
/**
* @author yu 2018/06/03.
*/
public class CopyDirVisitor extends SimpleFileVisitor {
private Path fromPath;
private Path toPath;
private StandardCopyOption copyOption;
public CopyDirVisitor(Path fromPath, Path toPath, StandardCopyOption copyOption) {
this.fromPath = fromPath;
this.toPath = toPath;
this.copyOption = copyOption;
}
public CopyDirVisitor(Path fromPath, Path toPath) {
this(fromPath, toPath, StandardCopyOption.REPLACE_EXISTING);
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Path targetPath = toPath.resolve(fromPath.relativize(dir));
if (!Files.exists(targetPath)) {
Files.createDirectory(targetPath);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, toPath.resolve(fromPath.relativize(file)), copyOption);
return FileVisitResult.CONTINUE;
}
}