简体   繁体   中英

How to filter resource files based on wild card using only JAVA APIs

I have several resource files located in META-INF folder in my spring project. I want to filter those files based on the file name using wild cards

Eg: Files located in META-INF folder as follows

a-config.xml
b-config.xml
c-config.xml

What I want is to filter these files using the wild card *-config.xml

I already know that I can easily use spring ResourcePatternResolver for this.

I tried to use java.io.FileFilter but even for that I have to pass a RegEx to filter the file names.

eg:

FileFilter customFilter = new FileFilter() {
      @Override
      public boolean accept(File pathname) {
        if(pathname.getName().matches("(.*)config(.*)")) {
           return true;
        }
     return false;
      }
    }; 

Are there any other ways to do this without using any third party dependencies (Spring,Google Guava etc) and RegEX? I want to use simple wild card characters to do the filtering. Your comments are highly appreciated.

what you want is a less-expressive pattern-matching known as Glob . one solution is to implement your own globbing using String class method (such as startsWith , endsWith ,...). but I think using regex is the best and general solution.

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