Properties以及语言的本地国际化配置
第一种
java.util.ResourceBundle messages = java.util.ResourceBundle.getBundle("messages");
java.util.ResourceBundle messages = java.util.ResourceBundle.getBundle("messages",Locale locale);//messages_ch.properties,java.util.Locale.getDefault()来得到本地默认的Locale, 大多数浏览器允许用户配置Locale。找到一个名为messages.properties的资源文件并自动载入资源,默认路径有两种default output folder(src),web-inf/classes目录。还可以实例化locale:Locale locale = new Locale(”zh”,”cn”)//messages_zh_cn.properties
String value = messages.getString(Key);
第二种
//读取key的value
Java.util.properties props = new java.util.properties();
Java.io.InputStream in = new java.io.InputStream(new FileInputStream(filepath));
//Java.io.bufferInputStream in = new BufferInputStream(new FileInputStream(filepath));
props.load(in);
String values = props.getProperty(key);
//读取properties全部信息
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty (key);
}
//写入properties信息
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + parameterName + "' value");
ResourceBundle中的中文乱码问题
解决方法如下:
通过Jdk/bin /native2ascii.exe//转换工具,将非Latin1和非Unicode的本地编码文件转换成Unicode编码的文件。
若messages.properties在硬盘的d://tests文件夹下:
在DOS下运行D:\tests>native2ascii.exe messages.properties andy.txt
然后用andy.txt文件的内容替换messages.properties文件就可以了。
Locale myLocale = Locale.getDefault();
System.out.println(myLocale.getCountry());//CN
System.out.println(myLocale.getLanguage());//zh
System.out.println(myLocale.getDisplayCountry());//中国
System.out.println(myLocale.getDisplayLanguage());//中文