Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible load all properties files contained in a package dynamically? i.e. MyClass.class.getResource('*.properties');

I am familiar with obtaining the contents of a properties file given the name of the file, and obviously MyClass.class.getResource('*.properties') will not work, but how can I obtain a list of ALL the properties files located in the same package as my class?

like image 875
BreakerOfStones Avatar asked Jun 02 '10 18:06

BreakerOfStones


1 Answers

Assuming that it's not JAR-packaged, you can use File#listFiles() for this. Here's a kickoff example:

String path = MyClass.class.getResource("").getPath();
File[] propertiesFiles = new File(path).listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.endsWith(".properties");
    }
});

If it's JAR-packaged, then you need a bit more code, to start with JarFile API. You can find another example in this answer.

like image 97
BalusC Avatar answered Nov 29 '22 03:11

BalusC