属性集概述和Properties类

属性集概述

Properties继承于Hashtable 来表示一个持久的属性集 它使用的键值结构存储数据 每个键及其对应值都是一个字符串 该类也被许多java类使用 比如获取系统属性 System.getProperties方法返回值就是一个Properties对象

Properties集合是一个唯一和Io流相结合的集合

可以使用Properties集合中的方法store 把集合中的临时数据 持久化写入到硬盘中存储

可以使用Properties集合中的方法load 把硬盘中保存的文件(键值对)读取到集合中使用

属性列表中每个集及其对应值都是一个字符串

Properties集合是一个双列集合 key和value默认都是字符串

Properties类

构造方法:

public Properties():创建一个空的属性列表

基本的存储方法:

public Object setProperty(String key,String value):保存一对属性
public String getProperty(String key):使用此属性列表中指定的键搜索属性值
public Set<String> stringPropertyNames():所以键的名称的集合

代码:

复制代码
public static void main(String[] args) {
        //创建Properties对象
        Properties pro = new Properties();
        //使用setProperty向集合中添加数据
        pro.setProperty("张三","法外狂徒");
        pro.setProperty("李四","叫嚣狂徒");
        pro.setProperty("王五","狂徒");
        //进行遍历
        Set<String> strings = pro.stringPropertyNames();
        for (String s : strings) {
            String value = pro.getProperty(s);
            System.out.println(s+"-->"+value);
        }
    }
复制代码

运行结果:

store 

 

 

 Properties不只可以这么写 还可以写进io流中将值持久化

可以使用Properties集合中的方法store,把集合中的临时数据 持久化写入到硬盘中存储

方法:

void store(OutputStream out,String comments);
void store(Writer writer,String comments);

参数:

OutputStream out:字节输出流 不能写入中文

Writer writer 字符输出流 可以写入中文

String comments:注释 用来解释说明保存的文件是做什么用的

不能使用中文会产生乱码 因为文本默认是Unicode编码 而编辑器默认是UTF-8编码

一般使用“空字符串”

使用步骤

1.创建Properties集合对象 添加数据

2.创建字节输出流/字符输出流对象 构造方法中绑定要输出的目的地

3.使用Properties集合中的方法store 把集合中的临时数据 持久化写入到硬盘中存储

4.释放资源

代码:

复制代码
 public static void main(String[] args) throws IOException {
        //创建Properties对象
        Properties pro = new Properties();
        //使用setProperty向集合中添加数据
        pro.setProperty("张三","法外狂徒");
        pro.setProperty("李四","叫嚣狂徒");
        pro.setProperty("王五","狂徒");
        //创建字符输出流 将集合中的键值对写入到文件中
        FileWriter fw = new FileWriter("D:\\file\\e.txt");
        //使用集合中的特有方法进行写入
        pro.store(fw,"");
        //关闭流释放资源
        fw.close();
    }
复制代码

运行前:

 

 

 运行结果:

 

 

 运行后

 

 

 e.txt:

load 

可以使用Properties集合中的方法load 把硬盘中保存的文件(键值对) 读取到集合中使用

void load(InputStream instream);
void load(Reader reader);

参数:

InputStream inStream:字节输入流 不能读取包含中文的减值对

Reader reader:字符输入流 能读取包含中文的键值对

使用步骤

1.创建Properties集合对象

2.使用Properties集合对象中的方法load读取保存键值对的文件

3.遍历Properties集合

注意:

1.存储键值对的文件中 键与值默认的链接符号可以使用=,空格(其他符号)

2.存储键值对的文件中 可以使用#进行注解 被注解的键值对不会再被读取

3.存储键值对的文件中 键与值默认都是字符串 不用再加引号

代码:

复制代码
public static void main(String[] args) throws Exception {
       //1.创建Properties集合对象
        Properties pro = new Properties();
        //2.使用Properties集合对象中的方法load读取保存键值对的文件
        pro.load(new FileReader("D:\\file\\e.txt"));
        //3.遍历Properties集合
        Set<String> set = pro.stringPropertyNames();
        for (String key : set) {
            String value = pro.getProperty(key);
            System.out.println(key+"-->"+value);
        }
    }
复制代码

运行结果:

posted @   baimingze  阅读(180)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示