基本介绍:
1、专门用于读写配置文件的集合类
配置文件格式:
键 = 值
键 = 值
2、键值对不需要有空格,值不需要引号括起来,默认类型是String
常用方法:
1.load:加载配置文件的键值对到Properties对象
2.list:将数据显示到指定设备
3.getProperty(key):根据键获取值
4.setProperty(key,value):设置键值对到Properties对象
5.store:将Properties中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,会存储为Unicode码
public class Study {
@SuppressWarnings({"all"})
public static void main(String[] args) throws Exception {
//创建Properties对象
Properties properties = new Properties();
//加载指定配置文件
properties.load(new FileReader("src\\mysql.properties"));
//把k-v显示到控制台
properties.list(System.out);
//根据key获取对应值
String user = properties.getProperty("user");
System.out.println("用户名:"+user);
//修改配置文件内容
//如果该文件没有这个key就是创建,如果有就是修改
properties.setProperty("user","汤姆");
properties.setProperty("password","20011110");
//再次存储到文件
properties.store(new FileWriter("src\\mysql.properties"),null);
System.out.println("保存配置文件成功...");
}
}