Java lookup extension

I need to expand the wildcards in the file path to get a list of files matching the file path.
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 extends only * or ? wildcards, but not ** wildcards, therefore: / usr / ** / *. xml does not match all files with extension .xml, in any subfolder of /usr .

How can I get a wildcard extension ** to work correctly?

thanks

+4
source share
1 answer

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

You can use FileUtils.iterateFiles or listFiles . Which uses one template for files and one template for directories. This is not exactly the same as a single expression for the globe:

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

Source: https://habr.com/ru/post/1342592/


All Articles