Java Utility Library

Java ResourceBundle - getKeys() Method



The java.util.ResourceBundle.getKeys() method returns an enumeration of the keys contained in this resource bundle and its parent bundles.

Syntax

public abstract Enumeration<String> getKeys()

Parameters

No parameter is required.

Return Value

Returns an Enumeration of the keys contained in this ResourceBundle and its parent bundles.

Exception

NA.

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!
message = How do you do!
bye = See you again!

In the below Java program, the java.util.ResourceBundle.getKeys() method is used to get an enumeration of the keys contained in given resource bundle and its parent bundles.

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);

    //get all keys present in the bundle
    Enumeration<String> e = bundle.getKeys();

    //printing all keys from enumeration
    while (e.hasMoreElements())
      System.out.println(e.nextElement());
  }
}

The output of the above code will be:

greet
message
bye

❮ Java.util - ResourceBundle