java.util.ResourceBundle 读取国际化资源或配置文件

1.定义三个资源文件,放到src的根目录下面

  命名规范是: 自定义名_语言代码_国别代码.properties
  默认 : 自定义名.properties
 
2.资源文件都必须是ISO-8859-1编码,因此,对于所有非西方语系的处理,都必须先将之转换为Java Unicode Escape格式。
转换方法是通过JDK自带的工具native2ascii.exe,直接输入中文后回车,或者如下:
  native2ascii -encoding gbk my.properties my_zh_CN.properties
 
3.读取
public class LoadMyproperties {
    public static void main(String[] args) { 
        Locale locale1 = new Locale("zh", "CN"); 
        ResourceBundle resb1 = ResourceBundle.getBundle("my", locale1); 
        System.out.println(resb1.getString("name")); 

        ResourceBundle resb2 = ResourceBundle.getBundle("my", Locale.getDefault()); 
        System.out.println(resb1.getString("name")); 

        Locale locale3 = new Locale("en", "US"); 
        ResourceBundle resb3 = ResourceBundle.getBundle("my", locale3); 
        System.out.println(resb3.getString("name")); 
    } 
}

 

 
 
posted @ 2013-12-09 18:28  等风来。。  Views(521)  Comments(0)    收藏  举报
------------------------------------------------------------------------------------------------------------ --------------- 欢迎联系 x.guan.ling@gmail.com--------------- ------------------------------------------------------------------------------------------------------------