拷贝整个文件树是可以递归每个目录和文件调用 Files.copy()
方法。在使用的时候有一下注意事项。
- 在往目录拷贝文件之前,首先要保证目录已经存在。拷贝源目录(不论是否为空)都会生成目标目录。整个任务一定在
preVisitDirectory()
方法中执行。 -
visitFile()
方式用来拷贝每个文件最佳的方法。 - 在你拷贝目录或文件时,你需要决定是否要使用
REPLACE_EXISTING
和COPY_ATTRIBUTES 选项。
- 如果你想保存要拷贝的目录的属性,你需要在
postVisitDirectory()
方法中做此操作。 - 如果你使用
FOLLOW_LINKS
选项,而在你的目录树中存在循环的链接指向父目录,这个循环将会在visitFileFailed()
方法中抛出FileSystemLoopException
异常。 - 如果文件不允许访问,可以根据你自己的决定来返回
FileVisitResult.CONTINUE或
TERMINATE
。 - 如果你指定
FOLLOW_LINKS
选项,则可以跟踪符号链接文件。
下面的例子使用C:\rafaelnadal
目录拷贝到C:\rafaelnadal_copy
。
import java.nio.file.FileSystemLoopException;
import java.nio.file.attribute.FileTime;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; class CopyTree implements FileVisitor { private final Path copyFrom;
private final Path copyTo; public CopyTree(Path copyFrom, Path copyTo) {
this.copyFrom = copyFrom;
this.copyTo = copyTo;
} static void copySubTree(Path copyFrom, Path copyTo) throws IOException {
try {
Files.copy(copyFrom, copyTo, REPLACE_EXISTING, COPY_ATTRIBUTES);
} catch (IOException e) {
System.err.println("Unable to copy " + copyFrom + " [" + e + "]");
} } @Override
public FileVisitResult postVisitDirectory(Object dir, IOException exc)
throws IOException {
if (exc == null) {
Path newdir = copyTo.resolve(copyFrom.relativize((Path) dir));
try {
FileTime time = Files.getLastModifiedTime((Path) dir);
Files.setLastModifiedTime(newdir, time);
} catch (IOException e) {
System.err.println("Unable to copy all attributes to: " + newdir+" ["+e+ "]");
}
} else {
throw exc;
} return FileVisitResult.CONTINUE;
} @Override
public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)
throws IOException {
System.out.println("Copy directory: " + (Path) dir);
Path newdir = copyTo.resolve(copyFrom.relativize((Path) dir));
try {
Files.copy((Path) dir, newdir, REPLACE_EXISTING, COPY_ATTRIBUTES);
} catch (IOException e) {
System.err.println("Unable to create " + newdir + " [" + e + "]");
return FileVisitResult.SKIP_SUBTREE;
} return FileVisitResult.CONTINUE;
} @Override
public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)
throws IOException {
System.out.println("Copy file: " + (Path) file);
copySubTree((Path) file, copyTo.resolve(copyFrom.relativize((Path) file)));
return FileVisitResult.CONTINUE;
} @Override
public FileVisitResult visitFileFailed(Object file, IOException exc)
throws IOException {
if (exc instanceof FileSystemLoopException) {
System.err.println("Cycle was detected: " + (Path) file);
} else {
System.err.println("Error occurred, unable to copy:" +(Path) file+" ["+ exc + "]");
} return FileVisitResult.CONTINUE;
}
} class Main { public static void main(String[] args) throws IOException { Path copyFrom = Paths.get("C:/rafaelnadal");
Path copyTo = Paths.get("C:/rafaelnadal_copy"); CopyTree walk = new CopyTree(copyFrom, copyTo);
EnumSet opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); Files.walkFileTree(copyFrom, opts, Integer.MAX_VALUE, walk);
}
}