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 which 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.
- ResourceBundle(): The default constructor which is mainly designed for use by the subclasses and the factory methods.
- clearCache(): This method deletes all resource bundles from the cache that were loaded by the default class loader.
static final void clearCache()
- containsKey(): This method returns true if the passed string argument is a key within the invoking resource bundle.
boolean containsKey(String k)
- 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)
- 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)
- 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)
- 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)
- 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)
- 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)
- getLocale(): This method returns the Locale associated with the current bundle.
public Locale getLocale()
- containsKey(): This method checks whether a given key exists within a resource bundle or its parent or not.
public boolean containsKey(String key)
- 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.
- ListResourceBundle(): The default constructor to create an object.
- getContents(): This method returns a two dimensional array that contains key/value pairs that represent resources.
protected abstract Object getContents()
- handleGetObject(): This method returns the object associated with the key in the current bundle if it exists.
public final Object handleGetObject(String key)
getKeys(): This method returns an enumeration of the keys in the resource bundle.
public Enumeration getKeys()
- 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 Programm String for StopText key: Anschlag String for StartText key: Anfang
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.