For this kind of purpose JDK includes the ResourceBundle!
look at the following code snippet, which reads a <className>.properties file within the relative dir called resources benath the package dir:
// just be clean on any OS
NL = System.getProperty("file.separator");
ClassLoader cl = Config.class.getClassLoader();
File baseDir = new File(cl.getResource(".").getFile());
// now we now the dir the class Object is actually running in
RUN_DIR = baseDir.getAbsolutePath() + NL;
// could work with Bundles too, but lets use properties
props = new Properties();
ResourceBundle rb = getResourceBundle(Config.class, Locale.getDefault());
// allow comments on the same line within the xx.properties and strip them here
for (
String key : rb.keySet()) {
String s = rb.getString(key);
if (s.contains("#")) {
s = s.substring(0, s.indexOf('#')).trim();
}
fill the Properties
props.put(key, s);
}
-----------
ResourceBundle getResourceBundle(Class<?> cls, Locale locale) {
String baseName = classBundleBaseName(cls);
ClassLoader cl = cls.getClassLoader();
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale, cl);
// could also be simply ResourceBundle.getBundle(baseName);
return bundle;
}