![](/img/trans.png)
[英]Files.move and Files.copy is throwing java.nio.file.FileAlreadyExistsException
[英]Java: Using nio Files.copy to Move Directory
我是 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
操作,它應該是一個新文件。 有人能指出我的操作哪里出了問題嗎? 謝謝!
您需要使用 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;
}
}
如果目標目錄不存在,只需制作目標目錄。
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()
也將返回目錄,您要么不希望遞歸到該目錄,要么忽略這些目錄...
這是我將目錄從源遞歸移動到目標的解決方案。 它就像一個魅力。
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);
}
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()));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.