alem0lars
alem0lars

Reputation: 690

Java wildcard expansion

I need to expand wildcards in a filepath to get a list of files that match the filepath.
I used commons-io from apache:

protected File[] wildcardResolution(File f) {
    File dir = f.getParentFile();
    FileFilter fileFilter = new WildcardFileFilter(f.getName());
    return dir.listFiles(fileFilter);
}

The problem is that it expands only * or ? wildcards but not ** wildcards, so: /usr/**/*.xml doesn't match all files with extension .xml, in any subfolder of /usr.

How can i get ** wildcard expansion to work properly?

Thanks

Upvotes: 4

Views: 3167

Answers (1)

Thomas Jung
Thomas Jung

Reputation: 33092

The problem with File.listFiles is that it does not list recursively.

You can use FileUtils.iterateFiles or listFiles. Which uses one pattern for the files and one pattern for the directories. Which is not exactly the same as one globbing expression:

Iterator iterateFiles = FileUtils.iterateFiles(
  new File("."), new WildcardFileFilter("*.xml"), TrueFileFilter.INSTANCE);
while(iterateFiles.hasNext()){
    System.out.println(iterateFiles.next());
}

Upvotes: 4

Related Questions