Java 遇到权限错误时的 SimpleFileVisitor 问题
Java SimpleFileVisitor Issue when encountering permission errors
我正在尝试在我的硬盘中搜索 mp4 文件并将它们复制到特定文件夹中。问题是我没有访问以下文件夹的权限:"C:\Documents and Settings"
,所以我的程序在遇到这些文件夹时停止而不是继续。
我试图创建一个黑名单,但它根本不起作用。
package S;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class C {
public static void main(String args[]) throws IOException {
Path dir = Paths.get("C:/");
Files.walkFileTree(dir, new FindJavaVisitor());
}
private static class FindJavaVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().contains(".mp4")) {
file.toFile().renameTo(new File("C:/MP4/"+file.toFile().getName()));
}
return FileVisitResult.CONTINUE;
}
}
}
你必须覆盖两个方法。
如文档所述:
Unless overridden, this method re-throws the I/O exception that prevented the file from being visited.
您还必须覆盖 postVisitDirectory()
method;它有两个参数,第二个是 IOException
。如果出现错误,第二个参数将不会为空,在这种情况下,再次来自文档:
Unless overridden, this method returns CONTINUE if the directory iteration completes without an I/O exception; otherwise this method re-throws the I/O exception that caused the iteration of the directory to terminate prematurely.
鉴于您的错误,第二个是您要覆盖的那个。
但是,我在您的代码中看到您这样做 file.toFile().renameTo()
。
不要使用这个。请改用 Files.move()
。
最后,你在迭代的同时也移动了......这不是一个很好的主意。回想一下目录中的文件列表,例如,与旧的 API 不同,它是动态填充的!
您应该创建一个 "renaming" Map<Path, Path>
并在您访问完所有内容后执行重命名。至少在这种情况下我会这样做。
我正在尝试在我的硬盘中搜索 mp4 文件并将它们复制到特定文件夹中。问题是我没有访问以下文件夹的权限:"C:\Documents and Settings"
,所以我的程序在遇到这些文件夹时停止而不是继续。
我试图创建一个黑名单,但它根本不起作用。
package S;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class C {
public static void main(String args[]) throws IOException {
Path dir = Paths.get("C:/");
Files.walkFileTree(dir, new FindJavaVisitor());
}
private static class FindJavaVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().contains(".mp4")) {
file.toFile().renameTo(new File("C:/MP4/"+file.toFile().getName()));
}
return FileVisitResult.CONTINUE;
}
}
}
你必须覆盖两个方法。
如文档所述:
Unless overridden, this method re-throws the I/O exception that prevented the file from being visited.
您还必须覆盖 postVisitDirectory()
method;它有两个参数,第二个是 IOException
。如果出现错误,第二个参数将不会为空,在这种情况下,再次来自文档:
Unless overridden, this method returns CONTINUE if the directory iteration completes without an I/O exception; otherwise this method re-throws the I/O exception that caused the iteration of the directory to terminate prematurely.
鉴于您的错误,第二个是您要覆盖的那个。
但是,我在您的代码中看到您这样做 file.toFile().renameTo()
。
不要使用这个。请改用 Files.move()
。
最后,你在迭代的同时也移动了......这不是一个很好的主意。回想一下目录中的文件列表,例如,与旧的 API 不同,它是动态填充的!
您应该创建一个 "renaming" Map<Path, Path>
并在您访问完所有内容后执行重命名。至少在这种情况下我会这样做。