ResourceBundle,Properties读取配置文件

来源:互联网 发布:网络优化工程师好学不 编辑:程序博客网 时间:2024/06/05 16:56

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的变量的值

注意:不能写成这样  rb = ResourceBundle.getBundle("config.properties");
否则会报错(找不到指定路径)