一.Properties集合的基本使用

properties 支持 List properties配置集合_java


properties 支持 List properties配置集合_学习_02

package com.itheima.demo04Properties;

import java.util.Properties;
import java.util.Set;

/*
    java.util.Properties集合 extends Hashtable<k,v>集合 implements Map<k,v>接口
    1.Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。
        Properties集合是一个和IO流相结合的集合
        我们可以使用Properties集合中的方法store,把集合中的临时数据,持久化到硬盘的文件中存储(持久化:内存==>硬盘)
        我们可以使用Properties集合中的方法load,把硬盘文件中保存的键值对,在读取到内存中的集合里边使用(硬盘==>内存)
    2.属性列表中每个键及其对应值都是一个字符串。
        Properties集合是一个双列集合,键和值默认都是String类型,不需要写泛型
 */
public class Demo01Properties {
    public static void main(String[] args) {
        /*
            集合的重点:会使用Properties集合存储数据,会遍历Properties集合把数据取出来
            Properties集合健和值默认都是String类型,有一些和String相关的特有方法
                Object setProperty(String key, String value) 往Properties集合中添加键值对,相当于Map集合的put方法
                String getProperty(String key) 根据key获取value值,相当于Map集合的get方法
                Set<String> stringPropertyNames() 返回此属性列表中的键集,相当于Map集合的keySet方法
         */
        //创建Properties集合对象
        Properties prop = new Properties();
        //使用setProperty往Properties集合中添加键值对
        prop.setProperty("柳岩","18");
        prop.setProperty("迪丽热巴","19");
        prop.setProperty("古力娜扎","20");
        prop.setProperty("赵丽颖","21");
        //使用stringPropertyNames方法取出Properties集合中所有key,存储到一个Set集合中返回
        Set<String> set = prop.stringPropertyNames();
        //遍历Set集合,获取Properties集合的每一个key
        for (String key : set) {
            //使用getProperty方法,根据key获取value
            String value = prop.getProperty(key);
            System.out.println(key+"="+value);
        }
    }
}

二.Properties集合中的方法store(了解-扩展)

properties 支持 List properties配置集合_键值对_03

package com.itheima.demo04Properties;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

/*
    Properties集合中的方法store(了解-扩展)
    我们可以使用Properties集合中的方法store,把集合中的临时数据,持久化到硬盘的文件中存储(持久化:内存==>硬盘)
    void store(OutputStream out, String comments)
    void store(Writer writer, String comments)
    参数:
        OutputStream out:传递字节输出流,不能写中文(乱码)
        Writer writer:传递字符输出流,可以写中文
        String comments:注释,解释说明写的文件是干什么用的,不能写中文,会出现乱码,默认Unicode编码表
    使用步骤:
        1.创建Properties集合对象,往集合中存储一些键值对
        2.使用Properties集合中的方法store,把集合中的临时数据,持久化到硬盘的文件中存储
 */
public class Demo02strore {
    public static void main(String[] args) throws IOException {
        //1.创建Properties集合对象,往集合中存储一些键值对
        Properties prop = new Properties();
        prop.setProperty("liuyan","18");
        prop.setProperty("迪丽热巴","19");
        prop.setProperty("古力娜扎","20");
        prop.setProperty("赵丽颖","21");
        //2.使用Properties集合中的方法store,把集合中的临时数据,持久化到硬盘的文件中存储
        /*FileOutputStream fis = new FileOutputStream("day11\\prop1.txt");
        prop.store(fis,"save data");
        fis.close();*/
        //流对象只使用一次,可以使用匿名对象,使用完毕会自动释放资源
        prop.store(new FileOutputStream("day11\\prop1.txt"),"save data");
        prop.store(new FileWriter("day11\\prop2.txt"),"save data");
        //注意:后在工作中把保存键值对的文件叫配置文件,一般都是以.properties结尾
        prop.store(new FileWriter("day11\\prop.properties"),"save data");
    }
}

三.Properties集合中的方法load(重点)

properties 支持 List properties配置集合_键值对_04

package com.itheima.demo04Properties;

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

/*
    Properties集合中的方法load(重点)
    我们可以使用Properties集合中的方法load,把硬盘文件中保存的键值对,在读取到内存中的集合里边使用(硬盘==>内存)
    void load(InputStream inStream) 参数传递字节输入流,不能读取含有中文的文件
    void load(Reader reader) 参数传递字符输入流,可以读取含有中文的文件
    使用步骤:
        1.创建Properties集合对象
        2.使用Properties集合中的方法load,把硬盘文件中保存的键值对,在读取到内存中的集合里边使用
        3.打印集合,查看结果
    注意:
        在存储键值对的配置文件中,可以使用#作为注释,被注释的键值对不会被读取
        在存储键值对的配置文件中,键与值中间可以使用=,空格等一些符号作为分隔符号
        在存储键值对的配置文件中,键与值默认都是String类型,不需要添加引号,会画蛇添足
 */
public class Demo03load {
    public static void main(String[] args) throws IOException {
        //1.创建Properties集合对象
        Properties prop = new Properties();
        //2.使用Properties集合中的方法load,把硬盘文件中保存的键值对,在读取到内存中的集合里边使用
        //prop.load(new FileInputStream("day11\\prop.properties"));
        prop.load(new FileReader("day11\\prop.properties"));
        //3.打印集合,查看结果
        System.out.println(prop);
    }
}

prop.properties

#save data
#Wed Mar 30 11:27:06 CST 2022
赵丽颖=21
liuyan=18
#古力娜扎=20
迪丽热巴=19
胡哥 18
"霍建华" 18