【Java的Properties配置文件】

【Java的Properties配置文件】,第1张

【Java的Properties配置文件】 Properties配置文件

基本介绍

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

应用案例
    使用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("保存配置文件成功~");

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5719088.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存