ResourceBundle 用于加载properties文件,并通过key获取文件中的值。
通过静态方法实例化ResourceBundle:
static ResourceBundle | getBundle(String baseName, Locale locale) |
baseName 由两部分组成:类路径+properties文件的基本名称
匹配的properties文件名依次为:
properties文件的基本名称+"_"+locale.getLanguage()+"_"+locale.getCountry+".properties"
properties文件的基本名称+"_"+locale.getLanguage()+".properties"
properties文件的基本名称+".properties"
ResourceBundle 在baseName指定的类路径下,按上面顺序依次查找并加载properites文件,直到成功加载某个properties文件或抛出未找到异常(MissingResourceException)
举例:
baseName= com.app.message Locale = Local.CHINA
则加载的properties文件为: 类路径com/app/下
message_zh_CN.properties
message_zh.properties
message.properties
加载成功后就可以获取properties文件中的信息:
boolean | containsKey(String key) 确定给定 key 是否包含在此 ResourceBundle 及其父包中。 |
abstract Enumeration<String> | getKeys() 返回键的枚举。 |
Locale | getLocale() 返回此资源包的语言环境。 |
Object | getObject(String key) 从此资源包或它的某个父包中获取给定键的对象。 |
String | getString(String key) 从此资源包或它的某个父包中获取给定键的字符串。 |
String[] | getStringArray(String key) 从此资源包或它的某个父包中获取给定键的字符串数组。 |
protected abstract Object | handleGetObject(String key) 从此资源包中获取给定键的对象。 |
protected Set<String> | handleKeySet() 返回只 包含在此 ResourceBundle 中的键的 Set 。 |
Set<String> | keySet() 返回此 ResourceBundle 及其父包中包含的所有键的 Set 。 |
protected void | setParent(ResourceBundle parent) 设置此包的父包。 |
ResourceBundle 一般用于提取国际化消息,根据Locale 的不同,而定位到不同的properties文件,这些properties文件的key都相同,值是用不同语言描述,因此可以获得不同的语言消息。
为了支持不同国家的语言,properties文件的内容需要转换为unicode编码(\uxxxx)。
eclipse 编辑properties 时,录入的中文会自动转换为unicode编码。
也可以使用命令:native2ascii
native2ascii -encoding 源编码格式 源文件 目标文件
native2ascii -encoding UTF-8 message_zh_CN_src.properties message_zh_CN.properties
也可以使用 ResourceBundle 来切换配置信息,
举例:new Local("jdbc","db1") 或new Local("jdbc","db2")
用来切换jdbc连接
public void testResourceBundler(){
//类路径 test下message.properties中配置:
// city = \u4E2D\u56FD\u6C88\u9633
ResourceBundle resultBundle = ResourceBundle.getBundle("test.message",Locale.CHINA);
String city = resultBundle.getString("city");
System.out.println(city);//中国沈阳
//类路径 test下config_jdbc_db1.properties中配置:
// jdbc.url=jdbc:oracle:thin:@192.168.14.14:1521:db01
resultBundle = ResourceBundle.getBundle("test.config",new Locale("jdbc","db1"));
String jdbcUrl = resultBundle.getString("jdbc.url");
System.out.println(jdbcUrl);//jdbc:oracle:thin:@192.168.14.14:1521:db01
//类路径 test下config_jdbc_db2.properties中配置:
// jdbc.url=jdbc:oracle:thin:@192.168.14.15:1521:db02
resultBundle = ResourceBundle.getBundle("test.config",new Locale("jdbc","db2"));
jdbcUrl = resultBundle.getString("jdbc.url");
System.out.println(jdbcUrl);//jdbc:oracle:thin:@192.168.14.15:1521:db02
}
MessageFormat 用于格式化字符串,如字符串中有{0},{1},等,使用参数代替。
public void testMessageFormat(){
Object params = new Object []{"china"};
MessageFormat messageFormat = new MessageFormat(" your city is {0}");
System.out.println(messageFormat.format(params));//your city is china
double num = 12345.123;
params = new Object []{num};
messageFormat = new MessageFormat("the number is {0,number}");
System.out.println(messageFormat.format(params));//the number is 12,345.123
messageFormat = new MessageFormat("the number is {0,number,integer}");
System.out.println(messageFormat.format(params));//the number is 12,345
messageFormat = new MessageFormat("the number is {0,number,currency}");
System.out.println(messageFormat.format(params));//the number is ¥12,345.12
messageFormat = new MessageFormat("the number is {0,number,percent}");
System.out.println(messageFormat.format(params));//the number is 1,234,512%
messageFormat = new MessageFormat("the number is {0,number,#.#}");
System.out.println(messageFormat.format(params));//the number is 12345.1
messageFormat = new MessageFormat("the number is {0,number,#.##%}");
System.out.println(messageFormat.format(params));//the number is 1234512.3%
messageFormat = new MessageFormat("the number is {0,number,#,###.##}");
System.out.println(messageFormat.format(params));//the number is 12,345.12
params = new Object []{new Date()};
messageFormat = new MessageFormat("current time is {0,date}");
System.out.println(messageFormat.format(params));//current time is 2014-4-3
messageFormat = new MessageFormat("current time is {0,date,short}");
System.out.println(messageFormat.format(params));//current time is 14-4-3
messageFormat = new MessageFormat("current time is {0,date,medium}");
System.out.println(messageFormat.format(params));//current time is 2014-4-3
messageFormat = new MessageFormat("current time is {0,date,long}");
System.out.println(messageFormat.format(params));//current time is 2014年4月3日
messageFormat = new MessageFormat("current time is {0,date,full}");
System.out.println(messageFormat.format(params));//current time is 2014年4月3日 星期四
messageFormat = new MessageFormat("current time is {0,date,yyyy-MM-dd hh:mm:ss}");
System.out.println(messageFormat.format(params));//current time is 2014-04-03 11:41:24
}