Java 在根文件夹(windows)下按掩码搜索文件时,为什么会看到NullPointerException
我研究了JavaNIO2的可能性 我知道我可以使用Java 在根文件夹(windows)下按掩码搜索文件时,为什么会看到NullPointerException,java,search,nullpointerexception,nio,nio2,Java,Search,Nullpointerexception,Nio,Nio2,我研究了JavaNIO2的可能性 我知道我可以使用FileVisitor界面搜索文件。为了实现这个功能,我使用glob模式 我的示例代码: 访客界面实现: class MyFileFindVisitor extends SimpleFileVisitor<Path> { private PathMatcher matcher; public MyFileFindVisitor(String pattern){ try { mat
FileVisitor
界面搜索文件。为了实现这个功能,我使用glob模式
我的示例代码:
访客界面实现:
class MyFileFindVisitor extends SimpleFileVisitor<Path> {
private PathMatcher matcher;
public MyFileFindVisitor(String pattern){
try {
matcher = FileSystems.getDefault().getPathMatcher(pattern);
} catch(IllegalArgumentException iae) {
System.err.println("Invalid pattern; did you forget to prefix \"glob:\"? (as in glob:*.java)");
System.exit(1);
}
}
public FileVisitResult visitFile(Path path, BasicFileAttributes fileAttributes){
find(path);
return FileVisitResult.CONTINUE;
}
private void find(Path path) {
Path name = path.getFileName();
if(matcher.matches(name))
System.out.println("Matching file:" + path.getFileName());
}
public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes fileAttributes){
find(path);
return FileVisitResult.CONTINUE;
}
}
main方法的此变体工作正常,但如果我更改:
Path startPath = Paths.get("E:\\folder");
与
我看到了以下几点:
Exception in thread "main" java.lang.NullPointerException
at sun.nio.fs.WindowsFileSystem$2.matches(WindowsFileSystem.java:312)
at io.nio.MyFileFindVisitor.find(FileTreeWalkFind.java:29)
at io.nio.MyFileFindVisitor.preVisitDirectory(FileTreeWalkFind.java:33)
at io.nio.MyFileFindVisitor.preVisitDirectory(FileTreeWalkFind.java:13)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:192)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:69)
at java.nio.file.Files.walkFileTree(Files.java:2600)
at java.nio.file.Files.walkFileTree(Files.java:2633)
at io.nio.FileTreeWalkFind.main(FileTreeWalkFind.java:42)
我不知道这个问题的原因
如何解决此问题?出现空指针异常的原因是,当访问者测试第一个路径(E:\)时,没有要测试的实际文件名-这是卷根目录。从JDK文档:
Path getFileName()
Returns the name of the file or directory denoted by this path as a Path object. The file name is the farthest element from the root in the directory hierarchy.
Returns:
a path representing the name of the file or directory, or null if this path has zero elements
在本例中,“元素”是指名称中的目录元素E:\'没有目录元素,因为它是卷的根目录
您不应该假定文件名总是不为null
private void find(Path path) {
Path name = path.getFileName();
if (name != null) {
if(matcher.matches(name)) {
System.out.println("Matching file:" + path.getFileName());
}
}
}
在浏览Windows文件系统时可能需要注意的其他事项包括:
- 受系统保护的目录,如回收站,您的walker可能无法进入这些目录
- NTFS连接点,可能导致递归目录在自身上循环,导致代码循环,直到堆或堆栈用完
Path getFileName()
Returns the name of the file or directory denoted by this path as a Path object. The file name is the farthest element from the root in the directory hierarchy.
Returns:
a path representing the name of the file or directory, or null if this path has zero elements
private void find(Path path) {
Path name = path.getFileName();
if (name != null) {
if(matcher.matches(name)) {
System.out.println("Matching file:" + path.getFileName());
}
}
}