java.util.ResourceBundle.getBundle(String baseName, Locale targetLocale, ResourceBundle.Control control)%uA0方法返回使用指定的基本名称,目标语言环境和控件,并调用者的类加载器获取资源包。
声明
以下是java.util.ResourceBundle.getBundle()方法的声明
public static final ResourceBundle getBundle(String baseName, Locale targetLocale,ResourceBundle.Control control)
参数
-
baseName%uA0--资源包,一个完全限定类名的基本名称
-
locale%uA0-- 对于这一个资源包所需的语言环境
-
control%uA0-- 这给信息为资源包加载进程的控制
返回值
此方法返回的语言环境的给定基本名称和语言环境的资源包
异常
-
NullPointerException%uA0-- 如果ꂺseName, locales 或 control 为%uA0null
-
MissingResourceException%uA0-- 如果可以找到在任何语言环境中指定的基本名称的资源包。
-
IllegalArgumentException%uA0--如果给定的控件不正确执行(例如,control.getCandidateLocales返回null)请注意,control验证是根据需要执行。
例子
下面的示例演示java.util.ResourceBundle.getBundle()方法的用法。
package com.yiibai import java.util.Locale import java.util.ResourceBundle import java.util.ResourceBundle.Control public class ResourceBundleDemo { public static void main(String[] args) { // create a new ResourceBundle.Control with default format ResourceBundle.Control rbc = ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT) // create a new ResourceBundle with default locale and a Control ResourceBundle bundle = ResourceBundle.getBundle("hello", Locale.US, rbc) // print the text assigned to key "hello" System.out.println("" + bundle.getString("hello")) } }
假设在你的CLASSPATH中资源文件hello_en_US.properties可用,包含以下内容。该文件将被用作输入到示例程序:
hello=Hello World!
让我们来编译和运行上面的程序,这将产生以下结果:
Hello World!