How do I use directory globbing in JDK7

前端 未结 2 1148
一整个雨季
一整个雨季 2020-12-16 11:38

I have been trying to use the new globbing feature in JDK7, starting from the documentation and examples

I can get globs such as \"glob:*.dat\" to work

2条回答
  •  别那么骄傲
    2020-12-16 12:20

    Here's a working example which displays all the zip files in any descendant directory of d:/:

    public static void main(String[] args) throws IOException {
        final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:d:/**/*.zip");
        Files.walkFileTree(Paths.get("d:/"), new SimpleFileVisitor() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (matcher.matches(file)) {
                    System.out.println(file);
                }
                return FileVisitResult.CONTINUE;
            }
    
            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    }
    

    As you see, using forward slashes works on Windows.

提交回复
热议问题