1.对Propertises属性集合的创建,保存一对键值,根据键搜索值,获取所有键名称的集合,然后就是遍历获取所有的键值对的总结。
private static void demo1() {
Properties po=new Properties();
//保存键值对
po.setProperty("照照","22");
po.setProperty("丽丽","23");
po.setProperty("佳佳","23");
//遍历键值对
Set<String> set=po.stringPropertyNames(); //获取所有键的集合
for (String key:set){
String value=po.getProperty(key); //获取键对应的值
System.out.println(key+"="+value);
}
}
2.Propertise属性集合中的store方法,把集合中中的数据持久化的写到硬盘中存储。
private static void demo2() throws IOException {
Properties po=new Properties();
//保存键值对
po.setProperty("照照","22");
po.setProperty("丽丽","23");
po.setProperty("佳佳","23");
//创建字节/字符输出流的对象,构造方法中绑定要输出的目的地
FileWriter fw=new FileWriter("day_04_code\\ab.txt");
//使用store方法,把集合中的方法持久化的写入到硬盘中
po.store(fw,"save data");
fw.close();
}
3.Propertise属性集合中的load方法,把硬盘中保存的文件读取到集合中使用。
private static void demo3() throws IOException {
//创建集合
Properties po=new Properties();
//可以使用字节输入流或者字符输入流读取文件,但是我们一般使用字符输入流
FileReader fr=new FileReader("day_04_code\\ab.txt");
//把文件中的数据读取到集合中
po.load(fr);
//已经读取成功了,我们可以遍历一下集合看下
Set<String> set = po.stringPropertyNames();
for (String key:set){
String value=po.getProperty(key);
System.out.println(key+"="+value);
}
}
4.如果对你有帮助,希望可以点个赞呦,如果热爱Java和算法,可以关注落花一叶知秋博主,博主会跟大家一起成长。