Michael
Michael

Reputation: 42100

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 ?

Upvotes: 1

Views: 612

Answers (1)

Lungu Daniel
Lungu Daniel

Reputation: 844

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());

Upvotes: 2

Related Questions