【Java】Properties类

在这里插入图片描述

01 初识Properties

在这里插入图片描述

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

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

还要进行其他操作,会比较麻烦,但用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 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //使用 Properties 类完成对 mysql.properties 的读取
        //1.创建Properties对象
        Properties properties = new Properties();
        //2.加载指定配置文件
        properties.load(new FileReader("D:\\Code\\Java\\files\\JavaSE\\src\\com\\study\\properties_\\mysql.properties"));
        //3.把k-v显示控制台
        properties.list(System.out);
        //4.根据key获取对应的值
        String user = properties.getProperty("user");
        String pwd = properties.getProperty("pwd");
        System.out.println("用户名: "+user);
        System.out.println(" 密码: "+pwd);
    }
}

在这里插入图片描述

  1. 使用 Properties 类添加 key - val 到新文件 mysql2.properties 中
 public static void main(String[] args) throws IOException {
    
    
        //使用 Properties 类添加 key - val 到新文件 mysql2.properties 中
        Properties properties = new Properties();
        //创建文件
        properties.setProperty("charset","utf8");
        properties.setProperty("user","孙悟空");//保存时,中文是以unicode码保存
        properties.setProperty("pwd","666");
        //将k-v存储到文件中,
        properties.store(new FileOutputStream("src\\mysql2.properties"),"hello world!");//右边是注释,可以null
        System.out.println("保存配置文件成功");
    }

在这里插入图片描述

  1. 使用 Properties 类完成对 mysql2.properties 的读取,并修改某个 key - val
public static void main(String[] args) throws IOException {
    
    
    //使用 Properties 类添加 key - val 到新文件 mysql2.properties 中
    Properties properties = new Properties();
    //创建文件
    //1.如果该文件没有key,就是创建
    //2.如果该文件有key,就是修改
    properties.setProperty("charset","utf8");
    properties.setProperty("user","孙悟空");
    properties.setProperty("pwd","88888888");
    //将k-v存储到文件中,
    properties.store(new FileOutputStream("src\\mysql2.properties"),"hello world!");
    System.out.println("文件修改成功");
    /*
        Properties父类是Hashtable,所以底层就是Hashtable核心方法
     */
}

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

猜你喜欢

转载自blog.csdn.net/Lov1_BYS/article/details/128664117