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.