Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties vs Resource Bundle

Tags:

java

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.

like image 955
Sunil Kumar Sahoo Avatar asked Aug 08 '11 06:08

Sunil Kumar Sahoo


People also ask

What are resource bundles?

A resource bundle is a set of properties files with the same base name and a language-specific suffix. For example, if you create file_en. properties and file_de. properties, IntelliJ IDEA will recognize and combine them into a resource bundle.

What is the use of ResourceBundle?

ResourceBundle class is used to store text and objects which are locale sensitive. Generally we use property files to store locale specific text and then represent them using ResourceBundle object. Following are the steps to use locale specific properties file in a java based application.

Where does ResourceBundle properties file go?

ResourceBundle property files contain locale-specific objects for use by Java classes. The ResourceBundle property file must be placed somewhere in the CLASSPATH . Typically this is best accomplished by placing the ResourceBundle properties file in the same directory as the gear message class that it maps to.

What is ResourceBundle in spring boot?

A resource bundle is a Java properties file that contains locale-specific data. It is a way of internationalizing Java applications by making the code locale-independent. Resouce bundles are organized into families with a common base name.


2 Answers

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.

like image 190
Jon Skeet Avatar answered Sep 21 '22 21:09

Jon Skeet


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.

like image 31
Pandiaraj Avatar answered Sep 24 '22 21:09

Pandiaraj