Properties vs Resource Bundle

Java

Java Problem Overview


I read about properties and resource bundle. But I was unable to get difference between these. When to use Properties file and when to use Resource bundle.

To load properties file use the following code

Properties tempProp = new Properties();
FileInputStream propsFile = new FileInputStream(xyz.properties);
tempProp.load(propsFile);

To load Resource bundle

ResourceBundle labels =
    ResourceBundle.getBundle("xyz", currentLocale);
Enumeration bundleKeys = labels.getKeys();

In both of the cases (in resource bundle and in Properites) we are using properties file. The one difference I found is that to store application specific data we use properties file and to use i18n data we use resource bundle. I don't know whether i am right or not.

I would like to know the use of the above two. What is the difference between these two.

Java Solutions


Solution 1 - Java

Yes, you're thinking along the right lines.

Resource bundles don't have to use property files - it's just one implementation (PropertyResourceBundle). A properties file is really just a string-to-string mapping - and that can be used for i18n, but doesn't have to be.

ResourceBundle gives you a consistent way of requesting the appropriate object (usually a string) for a particular locale, with fallbacks etc. This is often, but not always, backed by a separate property file for each language.

So yes: if you're dealing with i18n, you should use ResourceBundle; if you just need a string-to-string map persisted in a text file, it's fine to use Properties directly.

Solution 2 - Java

ResourceBundle helps to load locale specific properties. If you have different properties file for each locale example DE,CN,etc ResourceBundle will load the appropriate locale specific file.

Solution 3 - Java

  1. Properties usually used for configuration file, not designed for internationalization (i18n) file. Such as log4j.properties or as your own configuration file.
  2. ResourceBundle is used for internationlization (i18n), the "magic" of ResourceBundle is not only key-value pair, but it can set to spesific Locale (language,region/country,variant) with the same key.

So, yes. it depends on what you need.

Solution 4 - Java

Resource Bundle: always considers your property files as part of the classpath. If you deploy the project, the property file actually goes into the actual deployment.

The Property Class loads the property files but the property files should not necessarily be in the classpath, or should not the part of the deployment file. it might be somewhere in the operating system or in some folder.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSunil Kumar SahooView Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaPandiarajView Answer on Stackoverflow
Solution 3 - JavaJohanView Answer on Stackoverflow
Solution 4 - JavaDapper DanView Answer on Stackoverflow