Java ResourceBundle Class



Introduction

The Java ResourceBundle class contain locale-specific objects.Following are the important points about ResourceBundle −

  • The class allows you to write programs that can be easily localized, or translated, into different languages.

  • This class programs handle multiple locales at once be easily modified later to support even more locales.

  • The Java Platform provides two subclasses of ResourceBundle, ListResourceBundle and PropertyResourceBundle.

Class declaration

Following is the declaration for java.util.ResourceBundle class −

public abstract class ResourceBundle
   extends Object

Field

Following are the fields for java.util.ResourceBundle class −

protected ResourceBundle parent − This is the parent bundle of this bundle.

Class constructors

Sr.No. Constructor & Description
1

ResourceBundle()

This is the single constructor.

Class methods

Sr.No. Method & Description
1 static void clearCache()

This method removes all resource bundles from the cache that have been loaded using the caller's class loader.

2 boolean containsKey(String key)

This method determines whether the given key is contained in this ResourceBundle or its parent bundles.

3 static ResourceBundle getBundle(String baseName)

This method gets a resource bundle using the specified base name, the default locale, and the caller's class loader.

4 abstract Enumeration<String> getKeys()

This method returns an enumeration of the keys.

5 Locale getLocale()

This method returns the locale of this resource bundle.

6 Object getObject(String key)

This method gets an object for the given key from this resource bundle or one of its parents.

7 String getString(String key)

This method gets a string for the given key from this resource bundle or one of its parents.

8 String[] getStringArray(String key)

This method gets a string array for the given key from this resource bundle or one of its parents.

9 protected abstract Object handleGetObject(String key)

This method gets an object for the given key from this resource bundle.

10 protected Set<String> handleKeySet()

This method queries if the given date is in daylight savings time in this time zone.

11 Set<String> keySet()

This method returns a Set of all keys contained in this ResourceBundle and its parent bundles.

12 protected void setParent(ResourceBundle parent)

This method sets the parent bundle of this bundle.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object

Getting Keys of a ResourceBundle Example

The following example shows the usage of Java ResourceBundle getKeys() method to print list of keys present in the properties file. We've created a resource bundle of US Locale for the corresponding hello_en_US.properties file. Then we printed the text assigned to key hello. Then the enumeration of keys is retrieved using getKeys() method and keys are printed.

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {
   public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US);

      // print the text assigned to key "hello"
      System.out.println("" + bundle.getString("hello"));

      // get the keys
      Enumeration<String> enumeration = bundle.getKeys();

      // print all the keys
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement());
      }
   }
}

Output

Assuming we have a resource file hello_en_US.properties available in your CLASSPATH, with the following content. This file will be used as an input for our example program −

hello = Hello World!
bye = Goodbye World!
morning = Good Morning World!
Advertisements