先前我们再读取配置文件时候,用的是:
Properties properties = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream stream = classLoader.getResourceAsStream("dba/config/db.properties");
if (null == stream) {
stream = BaseDAO.class.getClassLoader().getResourceAsStream("dba/config/db.properties");
}
// InputStream fis = new FileInputStream("/config.properties");
properties.load(stream);
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
要写一大堆的代码,今天在网上看见了另外一种方法,写出来与大家一起分享:
Locale locale3 = new Locale("en", "US");
ResourceBundle resb3 = ResourceBundle.getBundle("myres", locale3);
System.out.println(resb3.getString("aaa"));
只需要这么点点代码就可以完成我们上面相同的功能,真是太好了,不过其底层的实现还是用地ClassLoader,并该方法支持Local,比较好控制中英文。