Java ResourceBundle - containsKey() Method
The java.util.ResourceBundle.containsKey() method is used to determine whether the given key is contained in this ResourceBundle or its parent bundles.
Syntax
public boolean containsKey(String key)
Parameters
key |
Specify the resource key. |
Return Value
void type.
Exception
Throws NullPointerException, if key is null.
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!
In the below Java program, the java.util.ResourceBundle.containsKey() method is used to check if a given key is contained in the above mentioned resource bundle Greetings_en_US.properties or 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); //printing the text assigned to "greet" //key in the bundle System.out.println(bundle.getString("greet")); //check if the bundle contains "greet" keyword System.out.println(bundle.containsKey("greet")); //check if the bundle contains "welcome" keyword System.out.println(bundle.containsKey("welcome")); } }
The output of the above code will be:
Hello World! true false
❮ Java.util - ResourceBundle