Properties集合(笔记)

一.Properties集合的基本使用

在这里插入图片描述
在这里插入图片描述

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(了解-扩展)

在这里插入图片描述

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(重点)

在这里插入图片描述

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旭子在努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值