我是nio类的新用户,无法将文件目录移动到新创建的目录。
我首先使用:
创建2个目录File sourceDir = new File(sourceDirStr); //this directory already exists
File destDir = new File(destDirectoryStr); //this is a new directory
然后我尝试使用以下命令将现有文件复制到新目录中:
Path destPath = destDir.toPath();
for (int i = 0; i < sourceSize; i++) {
Path sourcePath = sourceDir.listFiles()[i].toPath();
Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName()));
}
这会引发以下错误:
Exception in thread "main" java.nio.file.FileSystemException: destDir/Experiment.log: Not a directory
我知道destDir/Experiment.log
不是现有目录;它应该是Files.copy
操作的结果的新文件。有人可以指出我的操作出错了吗?谢谢!
答案 0 :(得分:13)
您需要使用walkFileTree来复制目录。如果在目录上使用Files.copy,则只会创建一个空目录。
从http://codingjunkie.net/java-7-copy-move/
获取/改编代码File src = new File("c:\\temp\\srctest");
File dest = new File("c:\\temp\\desttest");
Path srcPath = src.toPath();
Path destPath = dest.toPath();
Files.walkFileTree(srcPath, new CopyDirVisitor(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING));
public static class CopyDirVisitor extends SimpleFileVisitor<Path>
{
private final Path fromPath;
private final Path toPath;
private final CopyOption copyOption;
public CopyDirVisitor(Path fromPath, Path toPath, CopyOption copyOption)
{
this.fromPath = fromPath;
this.toPath = toPath;
this.copyOption = copyOption;
}
@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;
}
}
答案 1 :(得分:4)
如果目标目录不存在,只需创建它。
File sourceDir = new File(source); //this directory already exists
File destDir = new File(dest); //this is a new directory
destDir.mkdirs(); // make sure that the dest directory exists
Path destPath = destDir.toPath();
for (File sourceFile : sourceDir.listFiles()) {
Path sourcePath = sourceFile.toPath();
Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName()));
}
请注意,sourceDir.listFiles()
也会返回您想要递归到的目录,或者忽略......
答案 2 :(得分:0)
for (int i = 0; i < sourceSize; i++) {
Path sourcePath = sourceDir.listFiles()[i].toPath();
Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName()));
}
这是非常奇怪的代码。您已经在sourceSize
的某处获得了文件计数,但是每次迭代都会调用listFiles()
。我本以期待更多这样的事情:
for (File file : sourceDir.listFiles()) {
Path sourcePath = file.toPath();
Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName()));
}
答案 3 :(得分:0)
这是我将目录从源递归移动到目标的解决方案。它像一种魅力。
public static void move(Path source, Path target) throws IOException {
class FileMover extends SimpleFileVisitor<Path> {
private Path source;
private Path target;
private FileMover(Path source, Path target) {
this.source = source;
this.target = target;
}
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
Files.move(file, target.resolve(source.relativize(file)),
StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
Path newDir = target.resolve(source.relativize(dir));
try {
Files.copy(dir, newDir,
StandardCopyOption.COPY_ATTRIBUTES,
StandardCopyOption.REPLACE_EXISTING);
} catch (DirectoryNotEmptyException e) {
// ignore and skip
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
Path newDir = target.resolve(source.relativize(dir));
FileTime time = Files.getLastModifiedTime(dir);
Files.setLastModifiedTime(newDir, time);
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
}
FileMover fm = new FileMover(source, target);
EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
Files.walkFileTree(source, opts, Integer.MAX_VALUE, fm);
}