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());
    }
}
相关推荐
yanghuashuiyue9 分钟前
lambda+sealed+record
java·开发语言
盟接之桥35 分钟前
盟接之桥EDI软件:API数据采集模块深度解析,打造企业数据协同新引擎
java·运维·服务器·网络·数据库·人工智能·制造
yzx9910131 小时前
Python数据结构入门指南:从基础到实践
开发语言·数据结构·python
HoneyMoose1 小时前
Spring Boot 2.4 部署你的第一个 Spring Boot 应用需要的环境
java
皮皮林5511 小时前
为什么 Spring 和 IDEA 都不推荐使用 @Autowired 注解??
java
衍生星球1 小时前
【JSP程序设计】Servlet对象 — page对象
java·开发语言·servlet·jsp·jsp程序设计
vx-Biye_Design1 小时前
servlet家政公司管理系统-计算机毕业设计源码01438
java·vue.js·spring·servlet·tomcat·maven·mybatis
云和数据.ChenGuang1 小时前
jenkins安装需要多少版本的jdk
java·servlet·jenkins
linux运维客栈1 小时前
Jenkins 最稳部署指南(含版本选择、Compose 部署、插件推荐)
java·servlet·jenkins
扶苏瑾1 小时前
线程安全问题的产生原因与解决方案
java·开发语言·jvm