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

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!