properties: Properties文件是java中很常用的一种配置文件,文件后缀为“.properties”,属文本文件,文件的内容格式是“键=值”的格式。
properties类 继承自Hashtable,而Hashtable又实现了Map接口,但是并不推荐使用put和putall方法,因为它们允许调用者插入其键或值不是 String 的项。应该使用 setProperty 方法,调用 store 或 save 方法。
读操作:
//Properties格式文件的读取
//创建文件的输入流
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\aaa\\ad.properties"))) {
Properties props = new Properties();
props.load(bis); //将输入流加载至Properties集合对象中
//根据key,获取value
System.out.println(props.get("cn"));
System.out.println(props.get("kr"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
写操作
try{
Properties props = new Properties();
props.put("f1","2344");
props.put("f2","3422");
try (//使用输出流,将Properties集合中的kv键值对,存入*.properties
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\音乐\\aaa\\demo.properties"))) {
props.store(bos,"just do it");
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}