假设我的类路径中有一些资源文件:
/a/b/c/x1.txt
/a/b/d/x2.txt
/a/b/e/x3.txt
我的代码不知道这些资源文件的完整路径。它仅知道其扩展名.txt
。您将如何编写一个函数来返回扩展名为.txt
的资源的路径数组?
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());