tags:

views:

119

answers:

1

I want to read a bunch of text files, by loading them as resources using the context classloader.

URL url = Thread.currentThread()
                .getContextClassLoader()
                .getResource("folder/foo.txt");

Is there some way to get a list of resources whose names match a given pattern? For eg:

URL[] matchingUrls = someLibrary.getMatchingResources("folder/*.txt");

Libraries like Spring can scan the classpath to find classes with a given annotation, so I am wondering if there something similar to load a bunch of resources.

+1  A: 

Spring supports ant-style class path resource matching.

http://static.springsource.org/spring/docs/2.5.x/reference/resources.html

Examples like : classpath:com/mycompany/**/applicationContext.xml, /WEB-INF/*-context.xml

See if you can use spring for your project. If it is not possible then you can always pull down the source code to see what they are doing, and do that yourself :)

Calm Storm
org.springframework.core.io.support.PathMatchingResourcePatternResolver.java seems to be the class doing the work. I cannot use Spring in my application, though. :(
binil