Java ResourceBundle containsKey() 方法



描述

java ResourceBundle containsKey(String key) 方法用于确定给定的键是否包含在此 ResourceBundle 或其父捆绑包中。

声明

以下是 java.util.ResourceBundle.containsKey() 方法的声明

public boolean containsKey(String key)

参数

key - 资源键

返回值

如果给定的键包含在此 ResourceBundle 或其父捆绑包中,则此方法返回 true;否则返回 false

异常

NullPointerException - 如果 key 为 null

检查键是否存在于美国区域设置的资源包中

以下示例演示了 Java ResourceBundle containsKey() 方法的用法,用于检查键是否存在于资源包中。我们为相应的 hello_en_US.properties 文件创建了一个美国区域设置的资源包。然后我们打印分配给键 hello 的文本。使用 containsKey() 方法,我们检查键“bye”和“hello”是否存在。

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"));

      // check if the bundle contains "bye" key
      System.out.println(bundle.containsKey("bye"));

      // check if the bundle contains "hello" key
      System.out.println(bundle.containsKey("hello"));
   }
}

输出

假设我们在你的 CLASSPATH 中有一个名为 hello_en_US.properties 的资源文件,其内容如下。此文件将用作我们示例程序的输入 -

hello = Hello World!

让我们编译并运行上述程序,这将产生以下结果 -

Hello World!
false
true

检查键是否存在于法国区域设置的资源包中

以下示例演示了 Java ResourceBundle containsKey() 方法的用法,用于检查键是否存在于资源包中。我们为相应的 hello_fr_FR.properties 文件创建了一个法国区域设置的资源包。然后我们打印分配给键 hello 的文本。使用 containsKey() 方法,我们检查键“bye”和“hello”是否存在。

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"));

      // check if the bundle contains "bye" key
      System.out.println(bundle.containsKey("bye"));

      // check if the bundle contains "hello" key
      System.out.println(bundle.containsKey("hello"));
   }
}

输出

假设我们在你的 CLASSPATH 中有一个名为 hello_fr_FR.properties 的资源文件,其内容如下。此文件将用作我们示例程序的输入 -

hello = Bonjour le monde!

让我们编译并运行上述程序,这将产生以下结果 -

Bonjour le monde!
false
true

检查键是否存在于德国区域设置的资源包中

以下示例演示了 Java ResourceBundle containsKey() 方法的用法,用于检查键是否存在于资源包中。我们为相应的 hello_de_DE.properties 文件创建了一个德国区域设置的资源包。然后我们打印分配给键 hello 的文本。使用 containsKey() 方法,我们检查键“bye”和“hello”是否存在。

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

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

      // check if the bundle contains "bye" key
      System.out.println(bundle.containsKey("bye"));

      // check if the bundle contains "hello" key
      System.out.println(bundle.containsKey("hello"));
   }
}

输出

假设我们在你的 CLASSPATH 中有一个名为 hello_de_DE.properties 的资源文件,其内容如下。此文件将用作我们示例程序的输入 -

hello = Hallo Welt!

让我们编译并运行上述程序,这将产生以下结果 -

Hallo Welt!
false
true
java_util_resourcebundle.htm
广告

© . All rights reserved.