Get a list of resources from classpath directory

前端 未结 14 918
予麋鹿
予麋鹿 2020-11-22 05:20

I am looking for a way to get a list of all resource names from a given classpath directory, something like a method List getResourceNames (String direct

14条回答
  •  攒了一身酷
    2020-11-22 05:52

    Using Reflections

    Get everything on the classpath:

    Reflections reflections = new Reflections(null, new ResourcesScanner());
    Set resourceList = reflections.getResources(x -> true);
    

    Another example - get all files with extension .csv from some.package:

    Reflections reflections = new Reflections("some.package", new ResourcesScanner());
    Set fileNames = reflections.getResources(Pattern.compile(".*\\.csv"));
    

提交回复
热议问题