Properties配置文件

基本介绍

- 专门用于读写配置文件的集合类
配置文件的的格式: 键=值 - 注意:键值对不需要有空格,值不需要引号,默认类型为String。
Properties常见的方法

应用案例
- 使用Properties类完成对mysql.properties的读取
Properties properties = new Properties();
properties.load(new FileReader("src\\mysql.properties"));
properties.list(System.out);

String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println("用户名 = " + user);
System.out.println("密码 = " + pwd);

- 使用Properties类添加或者修改K-V到新文件mysql2.properties中
Properties properties = new Properties();
properties.setProperty("charset", "utf-8");
properties.setProperty("user", "Tom");
properties.setProperty("pwd", "aabbcc");
properties.store(new FileOutputStream("src\\mysql2.properties"), null);
System.out.println("保存配置文件成功~");

|