-----------------ResourceBundle读取配置文件-------------------------------
测试及验证
下面我们来模拟一个多语言的环境
定义四个资源文件:res_en_US.properties、res_zh_CN.properties、res_zh.properties、res.properties
res_en_US.properties:cancelKey=cancel
res_zh_CN.properties:cancelKey=\u53D6\u6D88(取消)
res_zh.properties:cancelKey=\u53D6\u6D88zh(取消zh)
res.properties:cancelKey=\u53D6\u6D88default(取消default)
命名规则按照:资源名+_语言_国别.properties,每个资源文件中定义了本地化的信息,那么系统如何取到对应的资源文件呢?
ResourceBundle bundle = ResourceBundle.getBundle("res", new Locale("zh", "CN"));
其中new Locale("zh", "CN");这个对象就告诉了程序你的本地化信息,就拿这个来说吧:程序首先会去classpath下寻找res_zh_CN.properties
若不存在,那么会去找res_zh.properties,若还是不存在,则会去寻找res.properties,要还是找不到的话,那么就该抛异常了:MissingResourceException
我们可以来写个测试程序验证一下:
package bundle.test;
import java.util.Locale;
import java.util.ResourceBundle;
public class BundleTest {
public static void main(String args[]) {
ResourceBundle bundle = ResourceBundle.getBundle("res", new Locale("zh", "CN"));
String cancel = bundle.getString("cancelKey");
System.out.println(cancel);
bundle = ResourceBundle.getBundle("res", Locale.US);
cancel = bundle.getString("cancelKey");
System.out.println(cancel);
bundle = ResourceBundle.getBundle("res", Locale.getDefault());
cancel = bundle.getString("cancelKey");
System.out.println(cancel);
bundle = ResourceBundle.getBundle("res", Locale.GERMAN);
cancel = bundle.getString("cancelKey");
System.out.println(cancel);
}
}
输出:
取消
cancel
取消
取消
这里前三个都在我们的预期范围之内,但是最后一个GERMAN,应该会去使用res.properties这个资源包吧?怎么使用了res_zh_CH.properties?
原来ResourceBundle为我们提供了一个fallback(也就是一个备用方案),这个备用方案就是根据当前系统的语言环境来得到的本地化信息。
所以若是找不到GERMAN的,之后就会去找CHINA了,所以找到了res_zh_CH.properties这个资源包
这点我也是看了源代码才明白的,下面就贴上一些关键的源代码:
ResourceBundle baseBundle = null;
for (Locale targetLocale = locale;
targetLocale != null;
targetLocale = control.getFallbackLocale(baseName, targetLocale)) {// 这里就是去拿备用方案的
// do something 我们暂时不关心
}
跟踪control.getFallbackLocale(baseName, targetLocale)看看备用方案到底是什么?
public Locale getFallbackLocale(String baseName, Locale locale) {
if (baseName == null) {
throw new NullPointerException();
}
Locale defaultLocale = Locale.getDefault();
return locale.equals(defaultLocale) ? null : defaultLocale;
}
--------------Java中getResourceAsStream的用法读取配置文件----------------------------
首先,Java中的getResourceAsStream有以下几种:
1. Class.getResourceAsStream(String path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从
ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。
2. Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由
ClassLoader获取资源。
3. ServletContext. getResourceAsStream(String path):默认从WebAPP根目录下取资源,Tomcat下path是否以’/'开头无所谓,
当然这和具体的容器实现有关。
4. Jsp下的application内置对象就是上面的ServletContext的一种实现。
其次,getResourceAsStream 用法大致有以下几种:
第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类me.class ,同时有资源文件myfile.xml
那么,应该有如下代码:
me.class.getResourceAsStream("myfile.xml");
第二:在me.class目录的子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.y.file 目录下有资源文件myfile.xml
那么,应该有如下代码:
me.class.getResourceAsStream("file/myfile.xml");
第三:不在me.class目录下,也不在子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.file 目录下有资源文件myfile.xml
那么,应该有如下代码:
me.class.getResourceAsStream("/com/x/file/myfile.xml");
总结一下,可能只是两种写法
第一:前面有 “ / ”
“ / ”代表了工程的根目录,例如工程名叫做myproject,“ / ”代表了myproject
me.class.getResourceAsStream("/com/x/file/myfile.xml");
第二:前面没有 “ / ”
代表当前类的目录
me.class.getResourceAsStream("myfile.xml");
me.class.getResourceAsStream("file/myfile.xml");