Get a list of resources from classpath directory

前端 未结 14 1177
予麋鹿
予麋鹿 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 06:00

    If you use apache commonsIO you can use for the filesystem (optionally with extension filter):

    Collection files = FileUtils.listFiles(new File("directory/"), null, false);
    

    and for resources/classpath:

    List files = IOUtils.readLines(MyClass.class.getClassLoader().getResourceAsStream("directory/"), Charsets.UTF_8);
    

    If you don't know if "directoy/" is in the filesystem or in resources you may add a

    if (new File("directory/").isDirectory())
    

    or

    if (MyClass.class.getClassLoader().getResource("directory/") != null)
    

    before the calls and use both in combination...

提交回复
热议问题