1什么是属性集 他是干什么用的:唯一和i/o流相结合的可存储读取的集合
2属性集的一些使用特性和使用方法:key和value都是字符串
实例操作:记住上面倒数两行的方法
package com.bed.properties;
import java.util.Properties;
import java.util.Set;
public class PropertiesPractice {
public static void main(String[] args) {
show01();
}
private static void show01() {
Properties pro=new java.util.Properties();//建立一个properties集合对象
pro.setProperty("bed1","9");//像集合中添加元素
pro.setProperty("bed2","29");
pro.setProperty("bed3","39");
//将pro集合中的键全部取出到set集合里(默认都是String类的)
Set<String> set = pro.stringPropertyNames();
//遍历set集合
for (String s : set) {
String value = pro.getProperty(s);
System.out.println(s+"="+value);
}
}
}
3Properties中的store方法:
作用:
方法与参数:
使用步骤:
代码实现:
public class PropertiesPractice {
public static void main(String[] args) throws IOException {
show01();
show02();
}
private static void show02() throws IOException {
Properties pro=new java.util.Properties();//建立一个properties集合对象
pro.setProperty("bed1","9");//像集合中添加元素
pro.setProperty("bed2","29");
pro.setProperty("bed3","39");
//使用集合中的方法store,将集合中的临时数据永久化写入到磁盘中存储。
//因为是用的匿名对象,使用完自动关闭,不用再释放资源
pro.store(new FileOutputStream("day06-code\\pro.txt"),"data save");
}
3Properties中的load方法:作用,使用方法参数,和store方法正相反
代码实现:
package com.bed.properties;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class LoadProperties {
public static void main(String[] args) throws IOException {
Properties prop=new Properties();
//指明读取的路径
prop.load(new FileReader("day06-code\\pro.txt"));
//创建 接收键的集合
Set<String> set = prop.stringPropertyNames();
for (String s : set) {
String value = prop.getProperty(s);
System.out.println(s+value);
}
}
}