java.util ResourceBundle is a class in Java that enables developers to manage application-specific resources like text, images, etc. It is designed to be a central repository for all application resources and facilitates their access in a localized manner.
The containsKey() method of java.util ResourceBundle checks whether the given key exists in the set of stored resources. If it exists, it returns true, else false.
Code Examples:
1. Example using default ResourceBundle package:
// import ResourceBundle class import java.util.ResourceBundle;
public class ResourceBundleExample { public static void main(String[] args) { // load default properties file ResourceBundle bundle = ResourceBundle.getBundle("messages");
// check if key exists if (bundle.containsKey("welcome.message")) { System.out.println(bundle.getString("welcome.message")); } } }
In this example, we have created an instance of ResourceBundle using the default package/library. We have retrieved a message key from the properties file and printed it if the key exists.
2. Example using custom ResourceBundle package:
// import ResourceBundle class import java.util.ResourceBundle;
public class CustomBundleExample { public static void main(String[] args) { // load custom properties file from custom package ResourceBundle bundle = ResourceBundle.getBundle("com.myapp.resources.messages");
// check if key exists if (bundle.containsKey("error.message")) { System.out.println(bundle.getString("error.message")); } } }
In this example, we have created an instance of ResourceBundle using a custom package/library. We have retrieved an error message key from the properties file and printed it if the key exists. The properties file is stored in a directory structure matching the package hierarchy.
Package/Library: java.util
Java ResourceBundle.containsKey - 22 examples found. These are the top rated real world Java examples of java.util.ResourceBundle.containsKey extracted from open source projects. You can rate examples to help us improve the quality of examples.