以Redis的配置文件redis.properties为例,配置文件默认路径为src路径
方式一 、通过Properties类来封装配置文件
方式
//1、 通过类加载器,获取配置文件的输入流。
InputStream in =
当前类(比如User).class.getClassLoader().getResourceAsStream("redis.properties");
//2、新建一个Properties对象
Properties properties = new Properties();
try {
pro.load(in); //properties对象封装配置文件的输入流,现在文件里面的信息都已被封装成String
} catch (IOException e) {
e.printStackTrace();
}
//3、获取配置文件.properties中的配置信息(伪代码)
String strvalue= properties.getProperty("配置文件中的KEY");
//4、如果是int类型
int intvalue = Integer.parseInt(strvalue);
方式二、 通过ResourceBundle类来封装配置文件
//1、通过ResourceBundle 来加载redis配置文件,不需要写后缀,不需要写后缀,不需要写后缀
ResourceBundle properties = ResourceBundle.getBundle("redis");
//2、获取配置文件.properties中的配置信息(伪代码)
String strvalue= properties.getProperty("配置文件中的KEY");
//3、如果是int类型
int intvalue = Integer.parseInt(
strvalue);