首页 > 编程语言 > 详细

JavaIO - Properties集合

时间:2020-03-11 13:42:32      阅读:69      评论:0      收藏:0      [点我收藏+]

Properties集合是双列集合

1,该集合中的键和值都是字符串类型

2,集合中的数据可以保存到流中或者从流中获取

通常该集合操作以键值对形式存在的配置文件。

● 获取输入流方法:

FileInputStream fis = new FileInputStream(properties文件路径);

InputStream is = 类名.class.getClassLoader().getResourceAsStream(properties文件名);

● 常用方法:

void load(InputStream in):从(文件)流中获取键值对信息,必须是字符串形式的键值对。

String getProperty(String Key):用指定的键在此属性列表中搜索属性,也就是通过Key得到Key所对应的Value。

void setProperty(String Key,String Value):存储元素,调用HashTable的put方法

void store(OutputStream out,String comments):将集合中的键值对数据写入输出流。comments文档注释

void list(PrintStream out):将集合中的键值输出到指定的输出流中

void clear():清空集合中所有键值对。

Set<String> StringPropertyNames():返回集合中的键集到Set集合

例:

store(OutputStream out,String comments):将集合中的键值对数据写入输出流。

Properties prop = new Properties();
// 存储元素
prop.setProperty("王五","25");
prop.setProperty("二狗","29");
prop.setProperty("李四","24");
prop.setProperty("赵六","16");
prop.setProperty("贝七","07");
FileOutputStream fos
=new FileOutputStream("PropertiesDemo.txt")
prop.store(fos,
"姓名年龄");
// 使用集合的store方法将键值对写入输出流, fos.close();关闭流资源

load(InputStream in):从(文件)流中获取键值对信息,必须是字符串形式的键值对。

FileInputStream fis = new FileInputStream("PropertiesDemo.txt");
Properties prop = new Properties();
prop.load(fis);
Set<String> names = prop.stringPropertyNames();
for(String name:names){   String value = prop.getProperty(name);   System.out.println(name+":"+value); }

JavaIO - Properties集合

原文:https://www.cnblogs.com/Dm920/p/12461838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!