- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java ResourceBundle getStringArray() Method
Description
The java.util.ResourceBundle.getStringArray(String key) method gets a string array for the given key from this resource bundle or one of its parents.
Declaration
Following is the declaration for java.util.ResourceBundle.getStringArray() method
public final String[] getStringArray(String key)
Parameters
key − the key for the desired string array
Return Value
This method returns the string for the given key array
Exception
NullPointerException − if key is null
MissingResourceException − if no object for the given key can be found
ClassCastException − if the object found for the given key is not a string
Getting All values as String Array from a Resource Bundle of US Locale Example
The following example shows the usage of Java ResourceBundle getStringArray() method. In this example, we're getting String Array of all available values in the properties file using getStringArray() method and printing them.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Enumeration; import java.util.Locale; import java.util.ResourceBundle; // this method seems to be having problems with the base implementation // the following example shows an alternative way doing the same function public class ResourceBundleDemo { public static String[] getPropertyStringArray(ResourceBundle bundle, Strin keyPrefix) { String[] result; Enumeration<String> keys = bundle.getKeys(); ArrayList<String> temp = new ArrayList<String>(); // get the keys and add them in a temporary ArrayList for (Enumeration<String> e = keys; keys.hasMoreElements();) { String key = e.nextElement(); if (key.startsWith(keyPrefix)) { temp.add(key); } } // create a string array based on the size of temporary ArrayList result = new String[temp.size()]; // store the bundle Strings in the StringArray for (int i = 0; i < temp.size(); i++) { result[i] = bundle.getString(temp.get(i)); } return result; } public static void main(String[] args) { // create a new ResourceBundle with specified locale ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US); // save the keys in a string array String[] s = ResourceBundleDemo.getPropertyStringArray(bundle, ""); // print the string array one by one for (int i = 0; i < s.length; i++) { System.out.println("" + s[i]); } } }
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! bye = Goodbye World! morning = Good Morning World!
Let us compile and run the above program, this will produce the following result −
Hello World! Goodbye World! Good Morning World!
Getting All values as String Array from a Resource Bundle of FRANCE Locale Example
The following example shows the usage of Java ResourceBundle getStringArray() method. In this example, we're getting String Array of all available values in the properties file using getStringArray() method and printing them.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Enumeration; import java.util.Locale; import java.util.ResourceBundle; // this method seems to be having problems with the base implementation // the following example shows an alternative way doing the same function public class ResourceBundleDemo { public static String[] getPropertyStringArray(ResourceBundle bundle, String keyPrefix) { String[] result; Enumeration<String> keys = bundle.getKeys(); ArrayList<String> temp = new ArrayList<String>(); // get the keys and add them in a temporary ArrayList for (Enumeration<String> e = keys; keys.hasMoreElements();) { String key = e.nextElement(); if (key.startsWith(keyPrefix)) { temp.add(key); } } // create a string array based on the size of temporary ArrayList result = new String[temp.size()]; // store the bundle Strings in the StringArray for (int i = 0; i < temp.size(); i++) { result[i] = bundle.getString(temp.get(i)); } return result; } public static void main(String[] args) { // create a new ResourceBundle with specified locale ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.FRANCE); // save the keys in a string array String[] s = ResourceBundleDemo.getPropertyStringArray(bundle, ""); // print the string array one by one for (int i = 0; i < s.length; i++) { System.out.println("" + s[i]); } } }
Output
Assuming we have a resource file hello_fr_FR.properties available in your CLASSPATH, with the following content. This file will be used as an input for our example program −
hello = Bonjour le monde! bye = Au revoir le monde! morning = Bonjour le monde!
Let us compile and run the above program, this will produce the following result −
Bonjour le monde! Au revoir le monde! Bonjour le monde!
Getting All values as String Array from a Resource Bundle of GERMAN Locale Example
The following example shows the usage of Java ResourceBundle getStringArray() method. In this example, we're getting String Array of all available values in the properties file using getStringArray() method and printing them.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Enumeration; import java.util.Locale; import java.util.ResourceBundle; // this method seems to be having problems with the base implementation // the following example shows an alternative way doing the same function public class ResourceBundleDemo { public static String[] getPropertyStringArray(ResourceBundle bundle, String keyPrefix) { String[] result; Enumeration<String> keys = bundle.getKeys(); ArrayList<String> temp = new ArrayList<String>(); // get the keys and add them in a temporary ArrayList for (Enumeration<String> e = keys; keys.hasMoreElements();) { String key = e.nextElement(); if (key.startsWith(keyPrefix)) { temp.add(key); } } // create a string array based on the size of temporary ArrayList result = new String[temp.size()]; // store the bundle Strings in the StringArray for (int i = 0; i < temp.size(); i++) { result[i] = bundle.getString(temp.get(i)); } return result; } public static void main(String[] args) { // create a new ResourceBundle with specified locale ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.GERMAN); // save the keys in a string array String[] s = ResourceBundleDemo.getPropertyStringArray(bundle, ""); // print the string array one by one for (int i = 0; i < s.length; i++) { System.out.println("" + s[i]); } } }
Output
Assuming we have a resource file hello_de_DE.properties available in your CLASSPATH, with the following content. This file will be used as an input for our example program −
hello = Hallo Welt! bye = Auf Wiedersehen Welt! morning = Guten Morgen Welt!
Let us compile and run the above program, this will produce the following result −
Hallo Welt! Auf Wiedersehen Welt! Guten Morgen Welt!
To Continue Learning Please Login
Login with Google