简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM