简体   繁体   English

如何通过扩展获取类路径资源?

[英]How to get classpath resources by extension?

Suppose I've got a few resource files in my classpath:假设我的类路径中有一些资源文件:

/a/b/c/x1.txt
/a/b/d/x2.txt
/a/b/e/x3.txt

My code does not know the full paths of these resource files.我的代码不知道这些资源文件的完整路径。 It knows only their extension .txt .它只知道它们的扩展名.txt
How would you write a function to return the paths of the resources with extension .txt ?您将如何编写 function 以返回扩展名为.txt的资源的路径?

Try this:尝试这个:

Path path = Paths.get(YourClass.class.getResource("/resources").toURI());

List<String> result = Files.find(path, 100,
        (p, a) -> p.toString().toLowerCase().endsWith(".txt"))
        .map(Path::toString)
        .collect(Collectors.toList());

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

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