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!
java_util_resourcebundle.htm
廣告