- 专门用于读写配置文件的集合类
配置文件的的格式:
键=值注意:键值对不需要有空格,值不需要引号,默认类型为String。
- 使用Properties类完成对mysql.properties的读取
//1. 创建properties Properties properties = new Properties(); //2. 加载指定配置文件 properties.load(new FileReader("src\mysql.properties")); //3. 把K-V显示到控制台 properties.list(System.out);
//4. 根据k-v获取对应的值 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 properties = new Properties(); //创建 //如果该文件没有key,则创建,有这个key,就是修改 properties.setProperty("charset", "utf-8"); properties.setProperty("user", "Tom"); properties.setProperty("pwd", "aabbcc"); //将K-V存储到文件中 // public void store(OutputStream out, String comments) properties.store(new FileOutputStream("src\mysql2.properties"), null); System.out.println("保存配置文件成功~");
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)