欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Java-I/O框架14:Properties集合及使用

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

2025/2/23 19:59:57 来源:https://blog.csdn.net/zhangjinajian759/article/details/143651272  浏览:    关键词:Java-I/O框架14:Properties集合及使用

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

1.Properties集合

特性:

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

2.Properties集合使用

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());}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词