Reading Property Files with ResourceBundle
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Bharath_Krishna
Posted On:   Thursday, December 19, 2002 06:31 AM

How to read from the a Property File using Resource Bundler

Re: Reading Property Files with ResourceBundle

Posted By:   Manlio_Marchica  
Posted On:   Thursday, December 19, 2002 07:24 AM

If you have the file "myConfile.properties" :


myProperty=ciao



and you want to read "myProperty":




ResourceBundle confFile = ResourceBundle.getBundle("myConfile");



property = confFile.getString("myProperty");



your properties file MUST be a list of pairs key=value (with no space)

Re: Reading Property Files with ResourceBundle

Posted By:   Christopher_Koenigsberg  
Posted On:   Thursday, December 19, 2002 07:17 AM

Put file named "myresources.properties" in "WEB-INF/classes" of your web app context, containing line "setting1=value1".



Then, for instance:




final String mylogname = "myClass.myInitializeMethod";
// Logger from either log4j or J2SE 1.4
Logger logger = getLogger(mylogname);
final String resourceBundleName = "myresources";
final String MY_DEFAULT_SETTING1 = "value0";
final String setting1name = "setting1";

ResourceBundle resetsettings = ResourceBundle.getBundle(resourceBundleName);
if (null == resetsettings) {
logger.warn(mylogname + " null ResourceBundle! Will use default settings.");
return;
} else {
logger.debug(mylogname + " got ResourceBundle OK.");
try {
String mysetting1 = resetsettings.getString(setting1name);
someObject.setMyValue1(mysetting1);
logger.debug(mylogname + " got " +
setting1name + " = " +
mysetting1 + "".");
} catch (MissingResourceException exc) {
someObject.setMyValue1(MY_DEFAULT_SETTING1);
logger.debug(mylogname + " No resource for name "" +
setting1name + "", using default "" +
MY_DEFAULT_SETTING1 + ""...");
}
}
....... [and so on] ....

About | Sitemap | Contact