英文:
How to get classpath resources by extension?
问题
假设我在类路径中有一些资源文件:
/a/b/c/x1.txt
/a/b/d/x2.txt
/a/b/e/x3.txt
我的代码并不知道这些资源文件的完整路径,它只知道它们的扩展名 .txt
。
您会如何编写一个函数来返回具有扩展名 .txt
的资源文件的路径?
英文:
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
.
How would you write a function to return the paths of the resources with extension .txt
?
答案1
得分: 2
尝试这样做:
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());
英文:
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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论