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.