Java ResourceBundle handleKeySet() 方法



描述

Java ResourceBundle handleKeySet() 方法僅返回此 ResourceBundle 中包含的鍵的 Set。

宣告

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

protected Set<String> handleKeySet()

引數

返回值

此方法返回此 ResourceBundle 中包含的鍵的 Set

異常

從美國區域設定的 ResourceBundle 獲取鍵的列舉示例

以下示例演示了 Java ResourceBundle handleKeySet() 方法的使用。在此示例中,我們擴充套件了 ResourceBundle 類並實現了 handleKeySet() 方法,並獲取了一組鍵作為 HashSet。

package com.tutorialspoint;

import java.util.*;

public class ResourceBundleDemo extends ResourceBundle {

   @Override
   public Object handleGetObject(String key) {
      if (key.equals("hello")) {
         return "Hello World!";
      } else {
         return null;
      }
   }

   @Override
   public Enumeration getKeys() {
      StringTokenizer key = new StringTokenizer("Hello World!");
      return key;
   }

   protected Set<String> handleKeySet() {
      return new HashSet<String>();
   }

   public static void main(String[] args) {

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

      // print the keys
      Enumeration<String> enumeration = bundle.getKeys();
      
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement());
      }
   }
}

輸出

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

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

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

hello
bye
morning

從法國區域設定的 ResourceBundle 獲取鍵的列舉示例

以下示例演示了 Java ResourceBundle handleKeySet() 方法的使用。在此示例中,我們擴充套件了 ResourceBundle 類並實現了 handleKeySet() 方法,並獲取了一組鍵作為 HashSet。

package com.tutorialspoint;

import java.util.*;

public class ResourceBundleDemo extends ResourceBundle {

   @Override
   public Object handleGetObject(String key) {
      if (key.equals("hello")) {
         return "Bonjour le monde!";
      } else {
         return null;
      }
   }

   @Override
   public Enumeration getKeys() {
      StringTokenizer key = new StringTokenizer("Bonjour le monde!");
      return key;
   }

   protected Set<String> handleKeySet() {
      return new HashSet<String>();
   }

   public static void main(String[] args) {

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

      // print the keys
      Enumeration<String> enumeration = bundle.getKeys();
      
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement());
      }
   }
}

輸出

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

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

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

hello
bye
morning

從德國區域設定的 ResourceBundle 獲取鍵的列舉示例

以下示例演示了 Java ResourceBundle handleKeySet() 方法的使用。在此示例中,我們擴充套件了 ResourceBundle 類並實現了 handleKeySet() 方法,並獲取了一組鍵作為 HashSet。

package com.tutorialspoint;

import java.util.*;

public class ResourceBundleDemo extends ResourceBundle {

   @Override
   public Object handleGetObject(String key) {
      if (key.equals("hello")) {
         return "Hallo Welt!";
      } else {
         return null;
      }
   }

   @Override
   public Enumeration getKeys() {
      StringTokenizer key = new StringTokenizer("Hallo Welt!");
      return key;
   }

   protected Set<String> handleKeySet() {
      return new HashSet<String>();
   }

   public static void main(String[] args) {

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

      // print the keys
      Enumeration<String> enumeration = bundle.getKeys();
      
      while (enumeration.hasMoreElements()) {
         System.out.println("" + enumeration.nextElement());
      }
   }
}

輸出

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

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

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

hello
bye
morning
java_util_resourcebundle.htm
廣告