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());
    }
}
相关推荐
Hy行者勇哥2 小时前
Python 与 VS Code 结合操作指南
开发语言·python
bingbingyihao2 小时前
多数据源 Demo
java·springboot
在努力的前端小白7 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
麦兜*8 小时前
Swift + Xcode 开发环境搭建终极指南
开发语言·ios·swiftui·xcode·swift·苹果vision pro·swift5.6.3
萧鼎9 小时前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
一叶飘零_sweeeet9 小时前
从繁琐到优雅:Java Lambda 表达式全解析与实战指南
java·lambda·java8
艾伦~耶格尔10 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
yujkss10 小时前
Python脚本每天爬取微博热搜-终版
开发语言·python
yzx99101310 小时前
小程序开发APP
开发语言·人工智能·python·yolo
一只叫煤球的猫10 小时前
🕰 一个案例带你彻底搞懂延迟双删
java·后端·面试