描述
這個java.util.ResourceBundle.getStringArray(String key)方法從此資源包或其父項之一獲取給定鍵的字符串數組。
聲明
以下是聲明java.util.ResourceBundle.getStringArray()方法
public final String[] getStringArray(String key)
參數
key- 所需字符串數組的鍵
返回值
此方法返回給定鍵數組的字符串
異常
NullPointerException- 如果鍵是null
MissingResourceException- 如果找不到給定鍵的對象
ClassCastException- 如果為給定鍵找到的對象不是字符串
示例
下麵的例子展示了 java.util.ResourceBundle.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]);
}
}
}
假設我們有一個資源文件hello_en_US.properties在您的 CLASSPATH 中可用,具有以下內容。該文件將用作我們示例程序的輸入 -
hello = Hello World! bye = Goodbye World! morning = Good Morning World!
讓我們編譯並運行上麵的程序,這將產生以下結果 -
Hello World! Goodbye World! Good Morning World!
相關用法
- Java Java.util.ResourceBundle.getString()用法及代碼示例
- Java Java.util.ResourceBundle.getBundle()用法及代碼示例
- Java Java.util.ResourceBundle.getKeys()用法及代碼示例
- Java Java.util.ResourceBundle.getObject()用法及代碼示例
- Java Java.util.ResourceBundle.getLocale()用法及代碼示例
- Java Java.util.ResourceBundle.Control.getControl()用法及代碼示例
- Java Java.util.ResourceBundle.Control.newBundle()用法及代碼示例
- Java Java.util.ResourceBundle.clearCache()用法及代碼示例
- Java Java.util.ResourceBundle.Control.getFormats()用法及代碼示例
- Java Java.util.ResourceBundle.handleGetObject()用法及代碼示例
- Java Java.util.ResourceBundle.containsKey()用法及代碼示例
- Java Java.util.ResourceBundle.keySet()用法及代碼示例
- Java Java.util.ResourceBundle.handleKeySet()用法及代碼示例
- Java Java.util.Random.nextInt()用法及代碼示例
- Java Java.util.Calendar.setTimeInMillis()用法及代碼示例
- Java Java.util.BitSet.andNot()用法及代碼示例
- Java Java.util.Calendar.getMinimum()用法及代碼示例
- Java Java.util.GregorianCalendar.isLeapYear()用法及代碼示例
- Java Java.util.ArrayList.remove()用法及代碼示例
- Java Java.util.ArrayList.size()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.util.ResourceBundle.getStringArray() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。