Java ResourceBundle getBundle() Method



Description

The java ResourceBundle getBundle(String baseName) method gets a resource bundle using the specified base name, the default locale, and the caller's class loader.

Declaration

Following is the declaration for java.util.ResourceBundle.getBundle() method

public static final ResourceBundle getBundle(String baseName)

Parameters

baseName − the base name of the resource bundle, a fully qualified class name

Return Value

This method returns a resource bundle for the given base name and the default locale

Exception

  • NullPointerException − if baseName is null

  • MissingResourceException − if no resource bundle for the specified base name can be found

Java ResourceBundle getBundle(String baseName,ResourceBundle.Control control)

Description

The Java ResourceBundle getBundle(String baseName,ResourceBundle.Control control) method returns a resource bundle using the specified base name, the default locale and the specified control.

Declaration

Following is the declaration for java.util.ResourceBundle.getBundle() method

public static final ResourceBundle getBundle(String baseName,ResourceBundle.Control control)

Parameters

  • baseName − the base name of the resource bundle, a fully qualified class name

  • control − the control which gives information for the resource bundle loading process

Return Value

This method returns a resource bundle for the given base name and the default locale

Exception

  • NullPointerException − if baseName or control is null

  • MissingResourceException − if no resource bundle for the specified base name can be found

  • IllegalArgumentException − if the given control doesn't perform properly (e.g., control.getCandidateLocales returns null.) Note that validation of control is performed as needed.

Java ResourceBundle getBundle(String baseName,Locale targetLocale,ClassLoader loader,ResourceBundle.Control control)

Description

The Java ResourceBundle getBundle(String baseName,Locale targetLocale,ClassLoader loader,ResourceBundle.Control control) method returns a resource bundle using the specified base name, target locale, class loader and control. Unlike the getBundle factory methods with no control argument, the given control specifies how to locate and instantiate resource bundles.

Declaration

Following is the declaration for java.util.ResourceBundle.getBundle() method

public static ResourceBundle getBundle(String baseName,Locale targetLocale,ClassLoader loader,ResourceBundle.Control control)

Parameters

  • baseName − the base name of the resource bundle, a fully qualified class name

  • locale − the locale for which a resource bundle is desired

  • loader − the class loader from which to load the resource bundle

  • control − the control which gives information for the resource bundle loading process

Return Value

This method returns a resource bundle for the given base name and locale

Exception

  • NullPointerException − if baseName,locale,loader orcontrol is null

  • MissingResourceException − if no resource bundle for the specified base name can be found

  • IllegalArgumentException − if the given control doesn't perform properly (e.g., control.getCandidateLocales returns null.) Note that validation of control is performed as needed.

Java ResourceBundle getBundle(String baseName,Locale locale,ClassLoader loader)

Description

The java ResourceBundle getBundle(String baseName,Locale locale,ClassLoader loader) method gets a resource bundle using the specified base name, locale, and class loader.

Declaration

Following is the declaration for java.util.ResourceBundle.getBundle() method

public static ResourceBundle getBundle(String baseName,Locale locale,ClassLoader loader)

Parameters

  • baseName − the base name of the resource bundle, a fully qualified class name

  • locale − the locale for which a resource bundle is desired

  • loader − the class loader from which to load the resource bundle

Return Value

This method returns a resource bundle for the given base name and locale

Exception

  • NullPointerException − if baseName,locale or loader is null

  • MissingResourceException − if no resource bundle for the specified base name can be found

Getting ResourceBundle using Base Name Example

The following example shows the usage of Java ResourceBundle getBundle(String baseName) method to get a resource bundle using base name only. We've created a resource bundle of default Locale (en_IN) for the corresponding hello_en_IN.properties file using basename as hello. Then we printed the text assigned to key hello.

package com.tutorialspoint;

import java.util.ResourceBundle;

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

      // create a new ResourceBundle with default locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello");

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

Output

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

hello = Hello World!

Let us compile and run the above program, this will produce the following result −

Hello World!

Getting ResourceBundle using Base Name and Resource Control Example

The following example shows the usage of Java ResourceBundle getBundle(String baseName, ResourceBundle.Control control) method to get a resource bundle using base name and Control with default format. First, we've created a Control object with default format.Then resource bundle of default Locale (en_IN) for the corresponding hello_en_IN.properties file using basename as hello and control object is created. Then we printed the text assigned to key hello.

package com.tutorialspoint;

import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;

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

      // create a new ResourceBundle.Control with default format
      ResourceBundle.Control rbc = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

      // create a new ResourceBundle with default locale and a Control
      ResourceBundle bundle = ResourceBundle.getBundle("hello", rbc);

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

Output

Let us compile and run the above program, this will produce the following result −

Hello World!

Getting ResourceBundle using Base Name, Locale, Loader and Control Example

The following example shows the usage of Java ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader, ResourceBundle.Control control) method to get a resource bundle using base name, given locale, class loader and Control with default format. First, we've created a Control object with default format. Then resource bundle of given Locale (en_US) for the corresponding hello_en_US.properties file using basename as hello and control object and a system class loader is created. Then we printed the text assigned to key hello.

package com.tutorialspoint;

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;

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

      ClassLoader cl = ClassLoader.getSystemClassLoader();

      // create a new ResourceBundle.Control with default format
      ResourceBundle.Control rbc = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

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

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

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!

Let us compile and run the above program, this will produce the following result −

Hello World!

Getting ResourceBundle using Base Name, Locale and Class loader Example

The following example shows the usage of Java ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) method to get a resource bundle using base name . First, we've created a Class Loader object. Then resource bundle of given Locale (en_US) for the corresponding hello_en_US.properties file using basename as hello, Locale as US and a system class loader is created. Then we printed the text assigned to key hello.

package com.tutorialspoint;

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

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

      ClassLoader cl = ClassLoader.getSystemClassLoader();

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

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

Output

Let us compile and run the above program, this will produce the following result −

Hello World!
java_util_resourcebundle.htm
Advertisements