简体   繁体   English

如何仅使用JAVA API基于通配符过滤资源文件

[英]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. 我的春季项目中的META-INF文件夹中有几个资源文件。 I want to filter those files based on the file name using wild cards 我想使用通配符根据文件名过滤那些文件

Eg: Files located in META-INF folder as follows 例如:位于META-INF文件夹中的文件如下

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

What I want is to filter these files using the wild card *-config.xml 我想要的是使用通配符*-config.xml过滤这些文件

I already know that I can easily use spring ResourcePatternResolver for this. 我已经知道我可以轻松地使用spring ResourcePatternResolver了。

I tried to use java.io.FileFilter but even for that I have to pass a RegEx to filter the file names. 我尝试使用java.io.FileFilter但即使如此,我也必须通过RegEx来过滤文件名。

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? 是否有其他方法可以执行此操作而无需使用任何第三方依赖项(Spring,Google Guava等)和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 . 您想要的是一种不太常见的模式匹配,称为Glob one solution is to implement your own globbing using String class method (such as startsWith , endsWith ,...). 一个解决方案是实现自己的globbing使用String类的方法(如startsWithendsWith ,...)。 but I think using regex is the best and general solution. 但是我认为使用regex是最好的通用解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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