Java Utility Library

Java ResourceBundle - getBundle() Method



The java.util.ResourceBundle.getBundle() method is used to get a resource bundle using the specified base name and locale, and the caller's class loader.

Syntax

public static final ResourceBundle getBundle(String baseName,
                                             Locale locale)

Parameters

baseName Specify the base name of the resource bundle, a fully qualified class name.
locale Specify the locale for which a resource bundle is desired.

Return Value

Returns a resource bundle for the given base name and locale.

Exception

  • Throws NullPointerException, if baseName or locale is null.
  • Throws MissingResourceException, if no resource bundle for the specified base name can be found.

Example:

Lets assume that we have resource file called Greetings_en_US.properties in the CLASSPATH with the following content. This file contains local message for US country.

greet = Hello World!

The example below shows how to use java.util.ResourceBundle.getBundle() method to get the resource bundle Greetings_en_US.properties in specified locale in a Java program.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a resource bundle in specified locale
    ResourceBundle bundle = ResourceBundle.getBundle("Greetings", Locale.US);

    //printing the text assigned to "greet" 
    //key in the bundle
    System.out.println(bundle.getBundle("greet"));   
  }
}

The output of the above code will be:

Hello World!

❮ Java.util - ResourceBundle