资源绑定器.
java.util包下提供的资源绑定器,便于获取属性配置文件(User.properties)中的内容
使用资源绑定器优点:
1、我们不再使用 IO流+Properties集合来获取配置文件中的内容了
2、不再通过先获取文件的绝对路径添加到Properties集合中来获取配置文件了
注意:
1、资源绑定器,只能绑定xxx.properties文件,并且这个文件必须在src类的根路径下开始,文件扩展名也必须是properties
2、并且在写路径的时候,路径后面的扩展名不能写
前期准备配置文件:
代码演示如下所示:
package bj.powernode.javase.reflect;
import java.util.ResourceBundle;
/*
资源绑定器
使用资源绑定器这种方式的时候,属性配置文件xxx.properties必须在src类的根路径下
*/
public class reflectTest06 {
public static void main(String[] args) {
// 资源绑定器,只能绑定xxx.properties配置文件,并且这个文件必须在src类的根路径下开始
// 并且写路径的时候,路径后面的扩展名.properties不能写
ResourceBundle rb =ResourceBundle.getBundle("Username");
// 当在包下的配置文件User.properties
// ResourceBundle rb1 =ResourceBundle.getBundle("bj\\powernode\\javase\\reflect\\User");
// 通过key获得value
String c =rb.getString("username");
System.out.println(c);
}
}
输出结果:
还没有评论,来说两句吧...