Load a Resource Bundle

This example shows how to load a ResourceBundle from the classpath and then enumerate through it.
Since ResourceBundle is an abstract class and therefore cannot be instantiated we call its static method getBundle() which returns the ResourceBundle instance.
The method has a few overloads but we use the one that takes a basename (in this case ‘Phrases’) and a locale. This means that somewhere on the classpath there has to be a file named ‘Phrases_en_US.properties’.
In this example the file only has two rows (see further below). An Enumeration with the keys is obtained by calling the getKeys() method, then a while-loop is used to enumerate through the keys and print out each of them along with the value.

This is what the contents of the Phrases_en_US.properties file looks like:

…and this is what the output looks like when the example is run:

Search for more Java info on this site here:
Custom Search

Please type any questions here.