Properties 类的使用 Properties 类的使用

Properties 类的使用

定义: 

  表示一个持久的集合,可以存在流中,或者从流中加载。是Hashtable子类,map集合方法都可以用。

方法的使用:

复制代码
/*
 * 集合对象 properties继承Hashtable实现了Map接口
 * 实现数据的永久存储 store
 * */
public class PropertiesDemo { public static void main(String[] args) throws Exception { fun3(); } //1.关于Properties集合 添加数据 获得数据 遍历数据 public static void fun1(){ Properties p=new Properties(); p.setProperty("a", "1"); p.setProperty("a1", "11"); p.setProperty("a2", "111"); //将集合中的键,存储到Set集合 Set<String> set=p.stringPropertyNames(); for(String k:set){ System.out.println(k+" "+p.getProperty(k)); } } //2.Properties集合特有的方法,load(InputStream in) load(Reader r) //流对象读取键值对 public static void fun2() throws Exception{ Properties p=new Properties(); FileReader f=new FileReader("c:\\1.properties"); p.load(f);//读取c:\\1.properties  f.close(); System.out.println(p); } //3.存 store(OutputStream out) store(Writer w) public static void fun3() throws IOException{ Properties p=new Properties(); p.setProperty("name", "张三"); p.setProperty("age", "22"); p.setProperty("name1", "张三"); //键不能相同,值可以相同,否则会覆盖 p.setProperty("name", "张三"); p.setProperty("name", "张三"); FileWriter f=new FileWriter("c:\\1.properties"); p.store(f, "");//存到c:\\1.properties  f.close(); } }
复制代码

猜你喜欢

转载自www.cnblogs.com/cuichaobo/p/10681329.html