首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Java中Properties集合

Java中Properties集合

作者头像
訾博ZiBo
发布2025-01-06 14:08:29
发布2025-01-06 14:08:29
22600
代码可运行
举报
运行总次数:0
代码可运行

一、Properties概述

1、API简介

①Properties 类表示了一个持久的属性集;

②Properties 可保存在流中或从流中加载;

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

2、概述

①Properties集合是唯一一个跟IO结合的集合;

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

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

④属性列表中每个键和对应的值都是一个字符串;

⑤Properties是双列集合,键和值默认都是字符串;

⑥Properties有一些操作字符串的特有方法;

3、Properties中的一些操作字符串的特有方法

①String getProperty(String key)

用指定的键在此属性列表中搜索属性;

②Object setProperty(String key, String value)

调用 Hashtable 的方法 put;

③Set<String> stringPropertyNames()

返回此属性列表中的键集;

4、代码演示

代码语言:javascript
代码运行次数:0
运行
复制
package study.io;

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

public class PropertiesTest {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("大哥","185");
        properties.setProperty("二哥","182");
        properties.setProperty("三哥","179");
        properties.setProperty("小弟","168");
        Set<String> strings = properties.stringPropertyNames();
        for (String key : strings) {
            System.out.println(key+"=="+properties.getProperty(key));
            //二哥==182
            //大哥==185
            //三哥==179
            //小弟==168
        }
    }
}

二、Properties中的store方法

1、作用

可以将内存中的临时数据写入到硬盘中存储;

2、store方法

①void store(OutputStream out, String comments)

以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流;

②void store(Writer writer, String comments)

以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符;

参数说明:

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

Writer:字符输出流,可写入中文;

String:注释(不能使用中文,会乱码,因为默认是Unicode编码,一般使用""空字符串),用来解释说明保存的文件是做什么用的;

3、使用步骤

①创建Properties集合对象,添加数据;

②创建字节输出流/字符输出流对象,构造方法中绑定输出的目的地;

③使用Properties中的store方法,可以将内存中的集合的临时数据写入到硬盘中存储;

④释放资源;

4、代码演示

代码:
代码语言:javascript
代码运行次数:0
运行
复制
package study.io;

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

public class PropertiesTest {
    public static void main(String[] args) throws IOException {
        //①创建Properties集合对象,添加数据;
        Properties properties = new Properties();
        properties.setProperty("大哥","185");
        properties.setProperty("二哥","182");
        properties.setProperty("三哥","179");
        properties.setProperty("小弟","168");
        //②创建字节输出流/字符输出流对象,构造方法中绑定输出的目的地;
        FileWriter fileWriter = new FileWriter("C:\\Users\\Administrator\\Desktop\\test.txt");
        //③使用Properties中的store方法,可以将内存中的集合的临时数据写入到硬盘中存储;
        properties.store(fileWriter,"");
        //④释放资源;
        fileWriter.close();
    }
}
运行结果:

三、Properties中的load方法(重点)

1、作用

把硬盘中保存的文件(键值对),读取到内存中使用;

2、load方法

①void load(InputStream inStream)

从输入流中读取属性列表(键和元素对);

②void load(Reader reader)

按简单的面向行的格式从输入字符流中读取属性列表(键和元素对);

参数说明:

InputStream:字节输入流,不能读取带有中文的键值对集合;

Reader:字符输入流,能读取带有中文的键值对集合;

3、使用步骤

①创建Properties集合对象;

②使用Properties集合对象的load方法读取保存键值对的文件;

③遍历Properties集合;

4、注意

①存储键值对的文件中,键与值默认的连接符号可以使用=,空格(或其他符号);

②存储键值对的文件中,可以使用#进行注释,被注释的键值对不对再被读取;

③存储键值对的文件中,键与值默认都字符串,不要再加引号;

5、代码演示

代码:
代码语言:javascript
代码运行次数:0
运行
复制
package study.io;

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

public class PropertiesTest {
    public static void main(String[] args) throws IOException {
        //①创建Properties集合对象;
        Properties properties = new Properties();
        //②使用Properties集合对象的load方法读取保存键值对的文件;
        FileReader fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\test.txt");
        properties.load(fileReader);
        //③遍历Properties集合;
        Set<String> set = properties.stringPropertyNames();
        for (String s : set) {
            System.out.println(s+"对应的值为"+properties.getProperty(s));
        }
    }
}
运行结果:
代码语言:javascript
代码运行次数:0
运行
复制
二哥对应的值为182
大哥对应的值为185
三哥对应的值为179
小弟对应的值为168
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Properties概述
    • 1、API简介
    • 2、概述
    • 3、Properties中的一些操作字符串的特有方法
    • 4、代码演示
    • 二、Properties中的store方法
      • 1、作用
      • 2、store方法
      • 3、使用步骤
      • 4、代码演示
    • 三、Properties中的load方法(重点)
      • 1、作用
      • 2、load方法
      • 3、使用步骤
      • 4、注意
      • 5、代码演示
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档