Java ResourceBundle keySet() 方法



描述

java ResourceBundle keySet() 方法返回此 ResourceBundle 及其父捆綁包中包含的所有鍵的集合。

宣告

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

public Set<String> keySet()

引數

返回值

此方法返回此 ResourceBundle 及其父捆綁包中包含的所有鍵的集合。

異常

從美國區域設定的屬性檔案中獲取鍵集

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

package com.tutorialspoint;

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
      System.out.println(bundle.keySet());
   }
}

輸出

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

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

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

Hello World!
[hello, bye, morning]

從法國區域設定的屬性檔案中獲取鍵集

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

package com.tutorialspoint;

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
      System.out.println(bundle.keySet());
   }
}

輸出

假設在您的 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 keySet() 方法的使用,用於列印屬性檔案中存在的鍵集。我們為相應的 hello_de_DE.properties 檔案建立了一個德國區域設定的資源包。然後,我們列印分配給鍵 hello 的文字。然後,使用 keySet() 方法檢索鍵集並列印。

package com.tutorialspoint;

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.GERMAN);

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

      // get the keys
      System.out.println(bundle.keySet());
   }
}

輸出

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

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

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

Hello World!
[hello, bye, morning]
java_util_resourcebundle.htm
廣告
© . All rights reserved.