Java ResourceBundle getKeys() 方法



描述

java ResourceBundle getKeys() 方法返回一個鍵的列舉。

宣告

以下是 java.util.ResourceBundle.getKeys() 方法的宣告

public abstract Enumeration<String> getKeys()

引數

返回值

此方法返回一個包含此 ResourceBundle 及其父捆綁包中包含的鍵的列舉。

異常

從美國區域設定的資源包中獲取所有鍵的列舉

以下示例演示了 Java ResourceBundle getKeys() 方法的使用,用於列印屬性檔案中存在的鍵列表。我們為相應的 hello_en_US.properties 檔案建立了一個美國區域設定的資源包。然後,我們列印分配給鍵 hello 的文字。然後,使用 getKeys() 方法檢索鍵的列舉並列印鍵。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {
   public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US);

      // print the text assigned to key "hello"
      System.out.println("" + bundle.getString("hello"));

      // get the keys
      Enumeration<String> enumeration = bundle.getKeys();

      // print all the keys
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement());
      }
   }
}

輸出

假設我們在您的 CLASSPATH 中有一個可用的資原始檔 hello_en_US.properties,其內容如下。此檔案將用作我們示例程式的輸入:

hello = Hello World!
bye = Goodbye World!
morning = Good Morning World!

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World!
hello
bye
morning

從法國區域設定的資源包中獲取所有鍵的列舉

以下示例演示了 Java ResourceBundle getKeys() 方法的使用,用於列印屬性檔案中存在的鍵列表。我們為相應的 hello_fr_FR.properties 檔案建立了一個法國區域設定的資源包。然後,我們列印分配給鍵 hello 的文字。然後,使用 getKeys() 方法檢索鍵的列舉並列印鍵。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {
   public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.FRANCE);

      // print the text assigned to key "hello"
      System.out.println("" + bundle.getString("hello"));

      // get the keys
      Enumeration<String> enumeration = bundle.getKeys();

      // print all the keys
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement());
      }
   }
}

輸出

假設我們在您的 CLASSPATH 中有一個可用的資原始檔 hello_fr_FR.properties,其內容如下。此檔案將用作我們示例程式的輸入:

hello = Bonjour le monde!
bye = Au revoir le monde!
morning = Bonjour le monde!

讓我們編譯並執行上述程式,這將產生以下結果:

Bonjour le monde!
hello
bye
morning

從德國區域設定的資源包中獲取所有鍵的列舉

以下示例演示了 Java ResourceBundle getKeys() 方法的使用,用於列印屬性檔案中存在的鍵列表。我們為相應的 hello_de_DE.properties 檔案建立了一個德國區域設定的資源包。然後,我們列印分配給鍵 hello 的文字。然後,使用 getKeys() 方法檢索鍵的列舉並列印鍵。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {
   public static void main(String[] args) {

      // create a new ResourceBundle with specified locale
      ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.GERMANY);

      // print the text assigned to key "hello"
      System.out.println("" + bundle.getString("hello"));

      // get the keys
      Enumeration<String> enumeration = bundle.getKeys();

      // print all the keys
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement());
      }
   }
}

輸出

假設我們在您的 CLASSPATH 中有一個可用的資原始檔 hello_de_DE.properties,其內容如下。此檔案將用作我們示例程式的輸入:

hello = Hallo Welt!
bye = Auf Wiedersehen Welt!
morning = Guten Morgen Welt!

讓我們編譯並執行上述程式,這將產生以下結果:

Hallo Welt!
hello
bye
morning
java_util_resourcebundle.htm
廣告

© . All rights reserved.