java.util.ResourceBundle.getString(String key) 方法获取一个字符串给定键从此资源包或它的某个父类。
声明
以下是java.util.ResourceBundle.getString()方法的声明
public final String getString(String key)
参数
-
key -- 键为所需的字符串
返回值
This method returns the string for the given key
异常
-
NullPointerException -- 如果 key 为 null
-
MissingResourceException -- 如果没有找到由给定键的对象
-
ClassCastException -- 如果发现给定键的对象不是一个字符串
例子
下面的示例演示java.util.ResourceBundle.getString()方法的用法。
package com.yiibai; 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")); } }
假设在你的CLASSPATH中资源文件hello_en_US.properties可用,包含以下内容。该文件将被用作输入到示例程序:
hello=Hello World!
让我们来编译和运行上面的程序,这将产生以下结果:
Hello World!