ResourceBundle and ListResourceBundle class in Java with Examples
The ResourceBundle and ListResourceBundle classes are part java.util package. These classes are designed to aid in the internationalization of programs.
ResourceBundle: The class ResourceBundle is an abstract class. It defines methods that enable you to manage a collection of locale-sensitive resources. Resource bundles are identified by their family name. To the family name is added a two-character lowercase language code that specifies the language. We can also specify a country code after the language code. It is a two-character uppercase identifier and is preceded by an underscore when linked to a resource bundle name.
1. ResourceBundle(): The default constructor which is mainly designed for use by the subclasses and the factory methods.
1. clearCache(): This method deletes all resource bundles from the cache that were loaded by the default class loader.
static final void clearCache()
2. containsKey(): This method returns true if the passed string argument is a key within the invoking resource bundle.
boolean containsKey(String k)
3. getBundle(): This method loads the resource bundle with the given name and the specified locale.
static final ResourceBundle getBundle(String familyName) static final ResourceBundle getBundle(String familyName, Locale loc)
4. setParent(): This method sets the passed bundle as parent of the invoking bundle. In case of a lookup, if the key is not found in the invoking object, then it is looked up in the parent bundle.
protected void setParent(ResourceBundle parent)
5. getObject(): This method retrieves and returns the Object associated with the key passed as argument either from the current resource bundle or the parent.
public final Object getObject(String key)
6. getHandleObject(): This method returns the object associated with the given key from the resource bundle. If no object is available null is returned.
protected abstract Object handleGetObject(String key)
7. getString(): This method retrieves and returns the string associated with the key passed as argument either from the current resource bundle or the parent.
public final String getString(String key)
8. getStringArray(): This method retrieves and returns the string array associated with the key passed as argument either from the current resource bundle or the parent.
public final String getStringArray(String key)
9. getLocale(): This method returns the Locale associated with the current bundle.
public Locale getLocale()
10. containsKey(): This method checks whether a given key exists within a resource bundle or its parent or not.
public boolean containsKey(String key)
11. keySet(): This method returns the set of all the keys in the current bundle or its parent bundle.
public Set keySet()
ListResourceBundle: It is a subclass of ResourceBundle. It is an abstract class, which manages the resources in an array of key/value pairs. It adds only one new method getContents(), which must be implemented by every subclass.
1. ListResourceBundle(): The default constructor to create an object.
1. getContents(): This method returns a two dimensional array that contains key/value pairs that represent resources.
protected abstract Object getContents()
2. handleGetObject(): This method returns the object associated with the key in the current bundle if it exists.
public final Object handleGetObject(String key)
3. getKeys(): This method returns an enumeration of the keys in the resource bundle.
public Enumeration getKeys()
4. handleKeySet(): This method returns the set of all the keys in the current resource bundle.
protected Set handleKeySet()
Example to demonstrate the use of a ResourceBundle
English Version: String for Title key: My Program String for StopText key: Stop String for StartText key: Start German Version String for Title key: Mein Program String for StopText key: Anschlag String for StartText key: Anfang
Please Login to comment...