Properties集合

这篇博客演示了Java中Properties集合的使用,包括如何添加、遍历属性,以及如何将属性保存到文件和从文件加载。示例代码展示了使用setProperty()、keySet()、entrySet()、stringPropertyNames()等方法,并通过PrintWriter和FileOutputStream进行文件操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Properties的特点:

存储属性名与属性值

属性名与属性值都是字符串类型

不存在泛型

该集合与流有关。可保存在流中或者从流中加载,属性列表中每个键及其对应的值都是一个字符串。

demo:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertiesCollection {
    public static void main(String[] args) {
        PropertiesDemo1();
    }

    public static void PropertiesDemo1() {
        // 创建集合
        Properties properties = new Properties();

        // 添加数据
        properties.setProperty("user_name :", "张三分");
        properties.setProperty("age :", "16");
        System.out.println(properties);

        // 遍历
        System.out.println("~~~~~~~~~~~~~key set~~~~~~~~~~~");
        Set<Object> proSet = properties.keySet();
        for(Object set : proSet) {
            System.out.println(set + " " + properties.get(set));
        }

        System.out.println("~~~~~~~~~~~~~entry set~~~~~~~~~~~");
        Set<Map.Entry<Object, Object>> entries = properties.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }

        System.out.println("~~~~~~~~~~~~~string property names~~~~~~~~~~~");
        Set<String> proName = properties.stringPropertyNames();
        for (String s:proName) {
            System.out.println(s + " " + properties.getProperty(s));
        }

        // 和流一起使用
        System.out.println("~~~~~~~list方法保存数据到文件~~~~~~~");
        try {
            PrintWriter pw = new PrintWriter("D:\\IntelliJ IDEA\\project\\PropertiesCollection\\src\\main\\java\\test.txt");
            properties.list(pw);
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("~~~~~~~~~~~store方法保存数据到文件~~~~~~~~~~~");
        try {
            FileOutputStream fos = new FileOutputStream("D:\\IntelliJ IDEA\\project\\PropertiesCollection\\src\\main\\java\\test.properties");
            properties.store(fos, "annotation");
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("~~~~~~~~~load方法从文件中加载数据~~~~~~~~");
        Properties properties1 = new Properties();
        try {
            FileInputStream fis = new FileInputStream("D:\\IntelliJ IDEA\\project\\PropertiesCollection\\src\\main\\java\\test.properties");
            properties1.load(fis);
            fis.close();
            System.out.println(properties1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值