ResourceBundle,Properties读取配置文件

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");
否则会报错(找不到指定路径)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值