【Java】Properties类

向右看齐 2023-09-23 22:13 179阅读 0赞

文章目录

  • 01 初识Properties
  • 02 Properties常用方法
  • 03 Properties使用案例

在这里插入图片描述

01 初识Properties

在这里插入图片描述

创建这样一个配置文件:
在这里插入图片描述
传统方法:

  1. public static void main(String[] args) throws IOException {
  2. //读取mysql.properties文件,并得到ip、user、pwd
  3. BufferedReader br = new BufferedReader(new FileReader("D:\\Code\\Java\\files\\JavaSE\\src\\com\\study\\properties_\\mysql.properties"));
  4. String line = "";
  5. while((line = br.readLine())!= null){
  6. //循环读取
  7. String[] split = line.split("=");
  8. System.out.println(split[0]+"值是:"+split[1]);
  9. }
  10. br.close();
  11. }

还要进行其他操作,会比较麻烦,但用Properties类来操作,就会方便很多

02 Properties常用方法

  • 是专门用于读写配置文件的集合类

    • 配置文件格式:
    • 键 = 值
    • 键 = 值
  • 注意:键值对不需要有空格,值不需要用引号;默认类型是String

常用方法:

  • load :加载配置文件的键值对到 Properties 对象;
  • list :将数据显示到指定设备(流对象);
  • getProperty ( key ) :根据键获取值;
  • setProperty ( key , value ) :设置键值对到 Properties 对象;
  • store :将 Properties 中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,会存储为 unicode码;

03 Properties使用案例

  1. 使用 Properties 类完成对 mysql.properties 的读取

    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Properties;

    public class Properties02 {

    1. public static void main(String[] args) throws IOException {
    2. //使用 Properties 类完成对 mysql.properties 的读取
    3. //1.创建Properties对象
    4. Properties properties = new Properties();
    5. //2.加载指定配置文件
    6. properties.load(new FileReader("D:\\Code\\Java\\files\\JavaSE\\src\\com\\study\\properties_\\mysql.properties"));
    7. //3.把k-v显示控制台
    8. properties.list(System.out);
    9. //4.根据key获取对应的值
    10. String user = properties.getProperty("user");
    11. String pwd = properties.getProperty("pwd");
    12. System.out.println("用户名: "+user);
    13. System.out.println(" 密码: "+pwd);
    14. }

    }

在这里插入图片描述

  1. 使用 Properties 类添加 key - val 到新文件 mysql2.properties 中

    public static void main(String[] args) throws IOException {

    1. //使用 Properties 类添加 key - val 到新文件 mysql2.properties 中
    2. Properties properties = new Properties();
    3. //创建文件
    4. properties.setProperty("charset","utf8");
    5. properties.setProperty("user","孙悟空");//保存时,中文是以unicode码保存
    6. properties.setProperty("pwd","666");
    7. //将k-v存储到文件中,
    8. properties.store(new FileOutputStream("src\\mysql2.properties"),"hello world!");//右边是注释,可以null
    9. System.out.println("保存配置文件成功");
    10. }

在这里插入图片描述

  1. 使用 Properties 类完成对 mysql2.properties 的读取,并修改某个 key - val

    public static void main(String[] args) throws IOException {

    1. //使用 Properties 类添加 key - val 到新文件 mysql2.properties 中
    2. Properties properties = new Properties();
    3. //创建文件
    4. //1.如果该文件没有key,就是创建
    5. //2.如果该文件有key,就是修改
    6. properties.setProperty("charset","utf8");
    7. properties.setProperty("user","孙悟空");
    8. properties.setProperty("pwd","88888888");
    9. //将k-v存储到文件中,
    10. properties.store(new FileOutputStream("src\\mysql2.properties"),"hello world!");
    11. System.out.println("文件修改成功");
    12. /*
    13. Properties父类是Hashtable,所以底层就是Hashtable核心方法
    14. */

    }

修改即是创建一个新的替换旧的:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,179人围观)

还没有评论,来说两句吧...

相关阅读