Java.util.ResourceBundle.containsKey() 方法
描述
java.util.ResourceBundle.containsKey(String key) 方法确定给定键是否包含在此 ResourceBundle 或其父包中。
声明
以下是 java.util.ResourceBundle.containsKey() 方法的声明
public boolean containsKey(String key)
参数
key − 资源键
返回值
如果给定键包含在此 ResourceBundle 或其父包中,则此方法返回 true; false 否则。
异常
NullPointerException − 如果键为 null
示例
下面的例子展示了 java.util.ResourceBundle.containsKey() 方法的使用。
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

