java resourcebundle properties_Java对properties文件操作(ResourceBundle类和Properties类)

1. 使用场景

properties文件多为配置信息,日常操作很频繁,例如测试框架中的接口配置文件,如下

test.url=http://localhost:8888

#登陆接口uri

#login.uri=/v1/login

login.uri=/login

#更新用户信息接口uri

updateUserInfo.uri=/v1/updateUserInfo

#获取用户列表接口uri

getUserList.uri=/v1/getUserInfo

#获取用户信息接口uri

getUserInfo.uri=/v1/getUserInfo

#添加用户接口uri

addUser.uri=/v1/addUser

那么,在写接口用例的时候,怎么获取到这些对应的测试数据呢?

b11509106fc0

示例

2. 第一种方法:ResourceBundle类

//第一种 ResourceBundle类

//1. 声明ResourceBundle并获取文件

ResourceBundle bundle = ResourceBundle.getBundle("application", Locale.CHINA);

//2. 获取值

String apiValue = bundle.getString("login.uri");

System.out.println("ResourceBundle类方式提取数据:" + apiValue);

3. 第二种方法:Properties类

//1. 文件路径

String dataPath = "src/main/resources/application.properties";

try {

//2.获取输入流

InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(dataPath)));

//3. 声明Properties类

Properties properties = new Properties();

//4. 从字节输入流中读取键值对。

properties.load(inputStream);

//5. 获取值

String string = properties.getProperty("login.uri");

System.out.println("Properties类提取数据:" + string);

} catch (IOException e) {

System.out.println("properties文件路径书写错误,请检查!");

}

b11509106fc0

控制台

4. ResourceBundle类和Properties类的差异

ResourceBundle类通常是用于针对不同的语言来使用的属性文件。

而如果你的应用程序中的属性文件只是一些配置,并不是针对多国语言的目的。那么使用Properties类就可以了。

通常可以把这些属性文件放在某个jar文件中。然后,通过调用class的getResourceAsStream方法,来获得该属性文件的流对象,再用Properties类的load方法来装载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值