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