%uA0java.util.ResourceBundle.getString(String key)%uA0方法获取一个字符串给定键从此资源包或它的某个父类。
声明
以下是java.util.ResourceBundle.getString()方法的声明
public final String getString(String key)
参数
-
key%uA0-- 键为所需的字符串
返回值
This method returns the string for the given key
异常
-
NullPointerException%uA0-- 如果%uA0key 为%uA0null
-
MissingResourceException%uA0-- 如果没有找到由给定键的对象
-
ClassCastException%uA0--%uA0如果发现给定键的对象不是一个字符串
例子
下面的示例演示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!