1

Java nio2 の可能性を調査します。

FileVisitorインターフェイスを使用してファイルを検索できることは知っていました。この機能を実現するために、グロブ パターンを使用します。

私の例のコード:

訪問者インターフェースの実現:

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;
    }
}

主な方法:

public static void main(String[] args) {
        Path startPath = Paths.get("E:\\folder");
        String pattern = "glob:*";
        try {
            Files.walkFileTree(startPath, new MyFileFindVisitor(pattern));
            System.out.println("File search completed!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

この main メソッドのバリアントは適切に機能しますが、変更すると:

Path startPath = Paths.get("E:\\folder");

Path startPath = Paths.get("E:\\"); 

次のスタックトレースが表示されます。

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)

私はこの問題の原因ではありません。

それを解決する方法は?

4

1 に答える 1

2

ヌル ポインター例外が発生する理由は、訪問者が最初のパス (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:\」はボリュームのルートであるため、ディレクトリ要素はありません。

filename が常に 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 ファイル システムを使用する際に注意が必要なその他の事項には、次のようなものがあります。

  • ごみ箱など、ウォーカーが降りることができないシステム保護ディレクトリ
  • NTFS ジャンクション ポイントにより、再帰的なディレクトリがループバックされ、ヒープまたはスタックが不足するまでコードがループする可能性があります
于 2014-08-12T00:01:27.507 に答える