Properties集合
Properties集合
Properties:属性集合
特点:
1.存储属性名和属性值
2.属性名和属性值都是字符串类型
3.没有泛型
4.和流有关
eg :
public class Demo02 {
public static void main(String[] args) throws Exception {
//1.创建集合
Properties properties = new Properties();
//2。添加数据
properties.setProperty("username","zhangsan");
properties.setProperty("age","20");
System.out.println(properties.toString());
//3.遍历
//3.1 keyset
//3.2 entryset
//3.3 StringPropertyNames()
Set<String> pronames = properties.stringPropertyNames();
for (String pro : pronames) {
System.out.println(pro+"==="+properties.getProperty(pro));
}
//4.和流有关的方法
// 1.list方法
//PrintWriter pw = new PrintWriter("g:\\print.txt");
//properties.list(pw);
//pw.close();
// 2.store方法 保存
//FileOutputStream fos = new FileOutputStream("g:\\store.properties");
//properties.store(fos,"注释");
//fos.close();
// 3.load方法 加载
Properties properties1 = new Properties();
FileInputStream fis = new FileInputStream("g:\\store.properties");
properties1.load(fis);
fis.close();
System.out.println(properties1.toString());
}
}

浙公网安备 33010602011771号