java.util.ResourceBundle.getBundle() - Retrieving Resource Bundles

This section provides a tutorial example on how to use the java.util.ResourceBundle.getBundle() method to retrieve resource bundle objects by resource bundle names.

In previous sections, we have created 3 resource bundles:

Now let's write a simple test program to use all 3 resource bundles in all 3 locales:

Here is a sample program that shows how to request a resource bundle and lookup the bundle object of the a given key and a given locale:

/* ResourceBundleTest.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.util.*;
public class ResourceBundleTest {
   public static void main(String[] a) {
      bundleClassTest();
      listBundleClassTest();
      propertyBundleFileTest();
   }
   public static void bundleClassTest() {
      ResourceBundle rb;
      Locale l;
      System.out.println("ResourceBundle Test:");
      System.out.println("The default resource:");
      rb = ResourceBundle.getBundle("ErrorMessage");
      System.out.println("  1:" + rb.getString("NO_FILE"));
      System.out.println("  2:" + rb.getString("FILE_EMPTY"));
      l = Locale.FRENCH;
      System.out.println("The French resource:");
      rb = ResourceBundle.getBundle("ErrorMessage",l);
      System.out.println("  1:" + rb.getString("NO_FILE"));
      System.out.println("  2:" + rb.getString("FILE_EMPTY"));
      l = new Locale("fr","CA");
      System.out.println("The French (Cananda) resource:");
      rb = ResourceBundle.getBundle("ErrorMessage",l);
      System.out.println("  1:" + rb.getString("NO_FILE"));
      System.out.println("  2:" + rb.getString("FILE_EMPTY"));
   }
   public static void listBundleClassTest() {
      ResourceBundle rb;
      Locale l;
      System.out.println("ListResourceBundle Test:");
      System.out.println("The default resource:");
      rb = ResourceBundle.getBundle("WarningMessage");
      System.out.println("  1:" + rb.getString("LAST_MINUTE"));
      System.out.println("  2:" + rb.getString("HIGH_LOAD"));
      l = Locale.FRENCH;
      System.out.println("The French resource:");
      rb = ResourceBundle.getBundle("WarningMessage",l);
      System.out.println("  1:" + rb.getString("LAST_MINUTE"));
      System.out.println("  2:" + rb.getString("HIGH_LOAD"));
      l = new Locale("fr","CA");
      System.out.println("The French (Cananda) resource:");
      rb = ResourceBundle.getBundle("WarningMessage",l);
      System.out.println("  1:" + rb.getString("LAST_MINUTE"));
      System.out.println("  2:" + rb.getString("HIGH_LOAD"));
   }
   public static void propertyBundleFileTest() {
      ResourceBundle rb;
      Locale l;
      System.out.println("PropertyResourceBundle Test:");
      System.out.println("The default resource:");
      rb = ResourceBundle.getBundle("NoteMessage");
      System.out.println("  1:" + rb.getString("STARTING_SYSTEM"));
      System.out.println("  2:" + rb.getString("LOADING_SETTINGS"));
      l = Locale.FRENCH;
      System.out.println("The French resource:");
      rb = ResourceBundle.getBundle("NoteMessage",l);
      System.out.println("  1:" + rb.getString("STARTING_SYSTEM"));
      System.out.println("  2:" + rb.getString("LOADING_SETTINGS"));
      l = new Locale("fr","CA");
      System.out.println("The French (Cananda) resource:");
      rb = ResourceBundle.getBundle("NoteMessage",l);
      System.out.println("  1:" + rb.getString("STARTING_SYSTEM"));
      System.out.println("  2:" + rb.getString("LOADING_SETTINGS"));
   }
}

Output:

ResourceBundle Test:
The default resource:
  1:File doesn't exist.
  2:File is empty.
The French resource:
  1:Document n'exist pas.
  2:Rien.
The French (Cananda) resource:
  1:Dossier n'exist pas.
  2:Rien.
ListResourceBundle Test:
The default resource:
  1:Bus is leaving within one minute.
  2:System load is very high.
The French resource:
  1:L'autobus part d'une minute.
  2:La charge de système est très haute.
The French (Cananda) resource:
  1:L'autobus part d'une minute.
  2:L'utilisation de système est très haute.
PropertyResourceBundle Test:
The default resource:
  1:Starting the system...
  2:Loading user's settings...
The French resource:
  1:Commencer le système...
  2:Les arrangements de l'utilisateur de chargement...
The French (Cananda) resource:
  1:Commencer le système...
  2:Commencer à charger les arrangements de l'utilisateur...

Note that:

Comparing the 3 different ways of internationalize text messages, the property file is the best:

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

Locales, Localization Methods and Resource Bundles

 java.util.Locale - Localization and Internationalization

 Locale Sensitive Operations

 java.util.ResourceBundle - Resource Bundle Abstract Class

 java.util.ListResourceBundle - Resource Bundles as Lists

 java.util.PropertyResourceBundle - Resource Bundles as Properties Files

java.util.ResourceBundle.getBundle() - Retrieving Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 PDF Printing Version