Properties与ResourceBundle都是用来读取配置文件的
Properties的处理方式是将其作为一个映射表,而且这个类表示了一个持久的属性集,他是继承HashTable这个类。(properties还可以用来写文件)
ResourceBundle本质上也是一个映射,但是它提供了国际化的功能。
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();//可以获取参数为key的变量的值
ResourceBundle读取配置文件:
ResourceBundle rb = ResourceBundle.getBundle("config");//这样会直接拿到配置文件(config.properties)中的内容
String value = rb.getString("key");//可以获取参数为key的变量的值