- Java.util 包类
- Java.util - 首页
- 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 包其他内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
Java ResourceBundle getStringArray() 方法
描述
java.util.ResourceBundle.getStringArray(String key) 方法从该资源包或其父级资源包中获取给定键的字符串数组。
声明
以下是 java.util.ResourceBundle.getStringArray() 方法的声明
public final String[] getStringArray(String key)
参数
key − 所需字符串数组的键
返回值
此方法返回给定键的字符串数组
异常
NullPointerException − 如果 key 为 null
MissingResourceException − 如果找不到给定键的对象
ClassCastException − 如果为给定键找到的对象不是字符串
从美国区域设置的资源包中获取所有值作为字符串数组的示例
以下示例演示了 Java ResourceBundle getStringArray() 方法的使用。在此示例中,我们使用 getStringArray() 方法获取属性文件中所有可用值的字符串数组,并打印它们。
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]); } } }
输出
假设在您的 CLASSPATH 中有一个名为 hello_en_US.properties 的资源文件,其内容如下。此文件将用作我们示例程序的输入 -
hello = Hello World! bye = Goodbye World! morning = Good Morning World!
让我们编译并运行上述程序,这将产生以下结果 -
Hello World! Goodbye World! Good Morning World!
从法国区域设置的资源包中获取所有值作为字符串数组的示例
以下示例演示了 Java ResourceBundle getStringArray() 方法的使用。在此示例中,我们使用 getStringArray() 方法获取属性文件中所有可用值的字符串数组,并打印它们。
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]); } } }
输出
假设在您的 CLASSPATH 中有一个名为 hello_fr_FR.properties 的资源文件,其内容如下。此文件将用作我们示例程序的输入 -
hello = Bonjour le monde! bye = Au revoir le monde! morning = Bonjour le monde!
让我们编译并运行上述程序,这将产生以下结果 -
Bonjour le monde! Au revoir le monde! Bonjour le monde!
从德国区域设置的资源包中获取所有值作为字符串数组的示例
以下示例演示了 Java ResourceBundle getStringArray() 方法的使用。在此示例中,我们使用 getStringArray() 方法获取属性文件中所有可用值的字符串数组,并打印它们。
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]); } } }
输出
假设在您的 CLASSPATH 中有一个名为 hello_de_DE.properties 的资源文件,其内容如下。此文件将用作我们示例程序的输入 -
hello = Hallo Welt! bye = Auf Wiedersehen Welt! morning = Guten Morgen Welt!
让我们编译并运行上述程序,这将产生以下结果 -
Hallo Welt! Auf Wiedersehen Welt! Guten Morgen Welt!