IO和Properties联合使用
IO流:文件的读和写。
Properties:是一个Map集合,key和value都是String类型
(反射机制的时候会用到)
IO+Properties的联合使用。
非常好的设计理念:
以后经常改变的数据,可以单独写到一个文件中,使用程序动态读取。
将来只需要修改这个文件的内容,java代码不需要改动,
不需要重新编译,服务器也不需要重启。就可以拿到动态的信息。
类似以上机制的这种文件被称为配置文件。
并且当配置文件中的内容格式是:
key=value
的时候,我们把这种配置文件叫做属性配置文件。
java规范中有要求:属性配置文件建议以.properties
结尾,但这不是必须的。
这种以.properties结尾的文件在java种被称为:属性配置文件。
其中Properties是专门存放属性配置文件内容的一个类。
当key重复会覆盖
public class IOPropertiesTest01 {
public static void main(String[] args) throws Exception {
/*
Properties是一个Map集合,key和value都是String类型
想将userinfo文件中的数据加载到Properties对象当中
*/
//新建一个输入流对象
FileReader reader = new FileReader("userinfo.properties");
//新建一个Map集合
Properties pro = new Properties();
//调用Properties对象的load方法将文件中的数据加载到Map集合中。
pro.load(reader);//文件中的数据顺着管道加载到Map集合当中,其中 = 左边做key,右边做value
//通过key来获取value
String username = pro.getProperty("username");
System.out.println(username);
String password = pro.getProperty("password");
System.out.println(password);
reader.close();
}
}
Properties的书写规范
#建议使用key=value的形式
username=admin
##########################在属性配置文件中#号是注释##############################
#属性配置文件中的key重复的话,value会自动覆盖!
#password=123456
password=123
#最好不要用空格 spring项目会出问题
data = 100
#不建议使用:号
#usernamex:admin