简体   繁体   中英

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 .
How would you write a function to return the paths of the resources with extension .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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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