ResourceBundle
ResourceBundle本质上也是一个映射,但是它提供了国际化的功能。
假设电脑设置的地区是中国大陆,语言是中文
那么你向ResourceBundle(资源约束名称为base)获取abc变量的值的时候,ResourceBundle会先后搜索
base_zh_CN_abc.properties
base_zh_CN.properties
base_zh.properties
base.properties
文件,直到找到abc为止
相应的,在英国就会去找base_en_GB_abc.properties等。
因此,你只需要提供不同语言的资源文件,而无需改变代码,就达到了国际化的目的。
另外,在.properties里面,不能直接使用中文之类文字,而是要通过native2ascii转乘\uxxxx这种形式
附:
1.编码问题:
无论系统的默认编码是什么,ResourceBundle在读取properties文件时统一使用iso8859-1编码。
因此,如果在默认编码为 GBK的系统中编写了包含中文的properties文件,经由ResourceBundle读入时,必须转换为GBK格式的编码,否则不能正确识别。src\resources\resourceBundle.properties
username=hello
pwd=root
zhname=小明
package resources;
import java.util.ResourceBundle;
public class ResourceBundleDemo {
public static void main(String[] args) {
//baseName是Properties文件在当前项目的相对全路径,不需要扩展名src\resources\resourceBundle.properties
ResourceBundle resourceBundle = ResourceBundle.getBundle("resources/resourceBundle");
String userName = resourceBundle.getString("username");
String pwd = resourceBundle.getString("pwd");
String zhName = resourceBundle.getString("zhname");
System.out.println(String.format("UserName:%s,Pwd:%s,中文名:%s", userName, pwd,zhName));
}
}
Output:
UserName:hello,Pwd:root,中文名:???÷
乱码原因:
在.properties里面,不能直接使用中文之类文字,而是要通过native2ascii转乘\uxxxx这种形式
/**
* Gets a resource bundle using the specified base name, the default locale,
* and the caller's class loader. Calling this method is equivalent to calling
* <blockquote>
* <code>getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())</code>,
* </blockquote>
* except that <code>getClassLoader()</code> is run with the security
* privileges of <code>ResourceBundle</code>.
* See {@link #getBundle(String, Locale, ClassLoader) getBundle}
* for a complete description of the search and instantiation strategy.
*
* @param baseName the base name of the resource bundle, a fully qualified class name
* @exception java.lang.NullPointerException
* if <code>baseName</code> is <code>null</code>
* @exception MissingResourceException
* if no resource bundle for the specified base name can be found
* @return a resource bundle for the given base name and the default locale
*/
public static final ResourceBundle getBundle(String baseName)
{
return getBundleImpl(baseName, Locale.getDefault(),
/* must determine loader here, else we break stack invariant */
getLoader(),
Control.INSTANCE);
}
与
ResourceBundle
ResourceBundle conf= ResourceBundle.getBundle("config/fnconfig/fnlogin");
String value= conf.getString("key"); Properties:
Properties prop = new Properties();
try {
InputStream is = getClass().getResourceAsStream("xmlPath.properties");
prop.load(is);
//或者直接prop.load(new FileInputStream("c:/xmlPath.properties"));
if (is != null) {
is.close();
}
} catch (Exception e) {
System.out.println( "file " + "catalogPath.properties" + " not found!\n" + e);
}
String value= prop.getProperty("key").toString();
http://oyzzhou.iteye.com/blog/854796
ResourceBundle的更多相关文章
- 错误 java.lang.ClassCastException: com.ylpw.sms.YZZYSenderUtil cannot be cast to ResourceBundle
出现错误: java.lang.ClassCastException: com.ylpw.sms.YZZYSenderUtil cannot be cast to ResourceBundle 百度搜 ...
- Java ResourceBundle类的使用
1.使用ResourceBundle读取配置文件 #数据库配置信息: DRIVER=com.mysql.jdbc.Driver URL=jdbc:mysql://localhost:3306/dmo ...
- 国际化,java.util.ResourceBundle使用详解
java.util.ResourceBundle使用详解 一.认识国际化资源文件 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译成不同的 ...
- 【转】java.util.ResourceBundle使用详解
原文链接:http://lavasoft.blog.51cto.com/62575/184605/ 人家写的太好了,条理清晰,表达准确. 一.认识国际化资源文件 这个类提供软件国际化的捷径.通 ...
- java.util.ResourceBundle使用详解
java.util.ResourceBundle使用详解 一.认识国际化资源文件 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译成不同的 ...
- 资源绑定ResourceBundle
package com.init; import java.util.ResourceBundle; public class Resources { /** * @param args */ pub ...
- [原创]java WEB学习笔记51:国际化 概述,API 之 locale类,dataFormat类,numberFormat类, MessageFormat类,ResourceBundle 类
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 使用ResourceBundle访问资源文件(properties)帮助类
import java.util.ResourceBundle; /** * 读取properties文件的帮助类 * @author */ public class PropertiesUtil { ...
- javaWEB国际化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用
DateFormat:格式化日期的工具类,本身是一个抽象类: NumberFormat:格式化 数字 到 数字字符串,或货币字符串的字符类; MessageFormat: 可以格式化模式字符串,模式字 ...
- java.util.ResourceBundle使用详解(转)
java.util.ResourceBundle使用详解 一.认识国际化资源文件 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译成不同的 ...
随机推荐
- Android定位功能(二)
在前文Android定位功能(一)中,已经大致介绍了一下在Android平台中,和定位功能相关的类,并举例获取了位置信息.但是前文是基于Criteria定制了一个标准,通过getBestProvide ...
- BackTrack5 (BT5)无线password破解教程WPA/WPA2-PSK无线password皴
昨天公布了BackTrack5 (BT5)无线weppassword破解教程之minidwep-gtk破解法一文,对BT5下破解wep无线password的简单方法做了介绍,今天奶牛为朋友们介绍下怎样 ...
- mongodb笔记2
成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...
- BST树,B树、B-树、B+树、B*树
BST树,B树.B-树.B+树.B*树 二叉搜索树(BST): 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树 ...
- POJ 2485 Highways (prim最小生成树)
对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...
- JSP-简单的练习省略显示长字符串
<%@ page contentType="text/html; charset=gb2312" %> <!-- JSP指令标签 --> <%@ pa ...
- hive的非交互模式
在linux的终端运行:$HIVE_HOME/bin/hive 会进入交互模式: $HIVE_HOME/bin/hive -e或者-f 是非交互模式 1.非交互模式运行HQL语句 $HIVE_HOM ...
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...
- Codeforces Helpful Maths
Xenia the beginner mathematician is a third year student at elementary school. She is now learning t ...
- HDU 5050 Divided Land(进制转换)
题意 给你两个二进制数m,n 求他们的最大公约数 用二进制表示 0<m,n<2^1000 先把二进制转换为十进制 求出最大公约数 再把结果转换为二进制 数比較大要用到大数 ...