Java-I/O框架14:Properties集合及使用

视频链接:16.32 Properties使用(2)_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1Tz4y1X7H7?spm_id_from=333.788.player.switch&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5&p=32

1.Properties集合

特性:

  • 存储属性名和属性值;
  • 属性名和属性值都是字符串类型;
  • 没有泛型;
  • 和流有关

2.Properties集合使用

java 复制代码
public class PropertiesDemo01 {
    public static void main(String[] args) throws Exception {

        //1创建集合
        Properties properties = new Properties();

        //2添加元素
        properties.setProperty("username","root");
        properties.setProperty("age","20");

        //3打印集合元素
        System.out.println(properties.toString());

        //4遍历集合
        //4.1------使用keySet()遍历------(自己补齐)
        //4.2------使用entrySet()遍历------(自己补齐)
        //4.3------使用stringPropertyNames()遍历------
        Set<String> propertyNames = properties.stringPropertyNames();
        for (String propertyName : propertyNames) {
            System.out.println(propertyName + "======" + properties.getProperty(propertyName));
        }

        //5和流有关的方法
        //5.1--------list()方法-------
        PrintWriter printWriter = new PrintWriter("C:\\kkk.txt");
        properties.list(printWriter);
        printWriter.close();

        //5.2--------store()方法,实现保存-------
        FileOutputStream fos = new FileOutputStream("C:\\kkk.properties");
        properties.store(fos,"Mr zhang");
        fos.close();

        //5.3--------load()方法,实现数据加载-------
        Properties properties1 = new Properties();
        FileInputStream fis = new FileInputStream("C:\\kkk.properties");
        properties1.load(fis);
        fis.close();
        System.out.println("--------------");
        System.out.println(properties1.toString());
    }
}
相关推荐
承渊政道8 分钟前
【Python编程—从入门到实践】(Python字典:从基础语义到现代工程实践)
开发语言·python·pycharm·字典·嵌套
学编程就要猛9 分钟前
流式编程及Spring中SSE实现
java·后端·spring·流式编程
随遇而安zx26 分钟前
【多线程】---ConcurrentHashMap 源码解析
java·多线程·并发
luj_176829 分钟前
一线一区一变破解人盯人防守密码
开发语言·网络·c++·经验分享·算法
不才不才不不才32 分钟前
Spring 源码系列(27): @Transactional 七大失效场景与源码归因
java·后端·spring
随遇而安zx35 分钟前
【多线程】---线程池 ThreadPoolExecutor 原理与调优
java·线程池·多线程
抠脚小弟38 分钟前
WebSocketServer 使用详解:从入门到实战
java·spring·websocketserver
随便做点啥43 分钟前
32卡64G-910B4-16后端集群部署报告
java·大数据·运维
名字还没想好☜1 小时前
Python 字符编码实战:encode/decode、UnicodeDecodeError 与 open 的 encoding 坑
开发语言·后端·python·编程语言
萧瑟余晖1 小时前
持久层选型与框架对比详解
开发语言·架构