ResourceBundle
properties文件命名一般为 自定义名_语言代码_国别代码.properties或自定义名.properties,如果两个文件都存在,优先使用前者,自定义名_zh_CN.properties。
Locale locale1 =
new
Locale(
"zh"
,
"CN"
);
ResourceBundle resb1 = ResourceBundle.getBundle( "myres" , locale1);
System.out.println(resb1.getString( "aaa" ));
ResourceBundle resb1 = ResourceBundle.getBundle( "myres" , locale1);
System.out.println(resb1.getString( "aaa" ));
如果没有提供语言和地区,使用系统默认的资源文件,Locale.getDefault()。资源文件必须是ISO-8859-1编码。对于所有非西方语系,都必须先转换为相应的格式。
native2ascii -encoding UTF-8(原资源编码) old.properties(原资源文件名) new.properties (新资源文件名)
java.util.MissingResourceException
配置文件放在classpath中。
Properties
Properties prop=new Properties();
prop.load(new FileInputStream("myres.properties"));
System.out.println(prop.getProperty("attr"));
prop.setProperty("name","value");
//若配置文件为xml
prop.loadFromXML(new FileInputStream("myres.xml"));
//读取jar包中的属性文件,通过调用class的getResourceAsStream(),再用Properties类的load方法装载。
prop.load(Classname.class.getResourceAsStream("myres.properties"));