如何使用指定的名称查找类路径中的所有资源? [英] How to locate all resources in classpath with a specified name?
问题描述
我想列出类路径中具有特定名称的所有文件。我期待多次出现,因此 Class.getResource(String)
将无效。
I want to list all files with a specific name in the class path. I am expecting multiple occurrences, hence Class.getResource(String)
will not work.
基本上我必须在类路径中的任何位置识别具有特定名称的所有文件(例如:xyz.properties),然后累计读取其中的元数据。
Basically I have to identify all files with a specific name (ex: xyz.properties) anywhere in the class path and then read the metadata in them cumulatively.
我想要一些效果 Collection< URL> Class.getResources(String)
但找不到类似的东西。
I want something of the effect Collection<URL> Class.getResources(String)
but could not find anything similar.
PS:我没有使用任何第三方库的奢侈因此需要一个本土解决方案。
PS: I don't have the luxury of using any third party libraries, hence in need of a home grown solution.
推荐答案
你可以使用枚举getResources(String name)实现相同。
例如:
Enumeration<URL> enumer = Thread.currentThread().getContextClassLoader().getResources("/Path/To/xyz.properties");
while (enumer.hasMoreElements()) {
System.out.print(enumer.nextElement());
}
这篇关于如何使用指定的名称查找类路径中的所有资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!