Suppose I have the following files:

/MyFiles/dir/a.txt

/MyFiles/dir/child/b.txt

In Ant, this is how you write code to output all text files in any subdirectory of a “dir” directory.

<fileset id="jb" dir="/MyFiles"> <include name="**/dir/**/*.txt" /> </fileset> <pathconvert pathsep="${line.separator}" property="out" refid="jb"/> <echo>${out}</echo>

In Java, the equivalent is

public class MyPathMatcher extends SimpleFileVisitor<Path> { private PathMatcher matcher = FileSystems.getDefault().getPathMatcher( "glob:**/dir/**.txt"); @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (matcher.matches(file)) { System.out.println("File " + file); } return FileVisitResult.CONTINUE; } public static void main(String[] args) throws Exception { MyPathMatcher dirs = new MyPathMatcher(); Files.walkFileTree(Paths.get("/MyFiles"), dirs); } }

The Java documentation says “Two asterisks, **, works like * but crosses directory boundaries. This syntax is generally used for matching complete paths”. Whereas in the Ant documentation, “When ** is used as the name of a directory in the pattern, it matches zero or more directories.”

The Ant approach probably feels more natural to me because I’ve been using it longer. But the Java approach seems more logical because it doesn’t have the extra slash that doesn’t actually get matched.