Java glob doesn't return results in root dir

Viewed 30

I've got this problem. When as a pattern, I enter "**/*.*" it results in listing every file in every subdir (as expected) but when entering "*.*" it prints zero results.

This is (not) my code, I copied it from some site and only added List<Path> part

import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class FileGlobNIO {
    public static List<Path> paths = new ArrayList<Path>();

    public static void match(String glob, String location) throws IOException {

        final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(
                glob);

        Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path,
                                             BasicFileAttributes attrs) throws IOException {
                if (pathMatcher.matches(path)) {
                    paths.add(path);
                    System.out.println(path);   // debug
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc)
                    throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    }

}

And this is main.java

import java.io.IOException;

public class Main {
    public static void main (String[] args) {
        String glob = "glob:**/*.*";
        String path = "C:/Users/Krzysiek/Desktop/";

        try {
            FileGlobNIO.match(glob, path);
            System.out.println(FileGlobNIO.paths.size());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Also where could I add return paths in the first file? Or is accessing it from the other file as FileGlobNIO.paths better?

1 Answers

Reading getPatchMatcher() it looks to me as the pattern you specified **/*.* selects only files that are in some subdirectory and have a name and extension.

If you really want all files on all levels, probably just specify **. The documentation says about this:

The ** characters matches zero or more characters crossing directory boundaries.

Edit:

I just tested some minimal code example and am surprised it is visiting all files on my C drive. It means I cannot reproduce your results.

public static void main(String[] args) throws Exception {
    PathMatcher pm = FileSystems.getDefault().getPathMatcher("glob:*.*");
    Files.walkFileTree(Paths.get("c:\\"), new FileVisitor<Path>() {
        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            System.out.println("preVisitDirectory("+dir+")");
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            System.out.println("visitFile("+file+")");
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            System.out.println("visitFileFailed("+file+", "+exc.getClass().getName()+": "+exc.getMessage()+")");
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            System.out.println("postVisitDirectory("+dir+", "+exc+")");
            return FileVisitResult.CONTINUE;
        }
    });
}
Related