java properties增删改查

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

java properties增删改查

 public static void main(String args[]) throws IOException {
  Properties prop = new Properties();
  OutputStream out = new FileOutputStream("D:\\workspace\\JavaStudy\\src\\test\\test.properties");
  
  /**
   * 新增逻辑:
   * 1.必须先读取文件原有内容
   * 2.增加新的记录以后,再一起保存
   */
  //1.先读取文件原有内容
  Map toSaveMap = new HashMap();
  Set keys = prop.keySet();
  for(Iterator itr = keys.iterator(); itr.hasNext();){
   String key = (String) itr.next();
   Object value = prop.get(key);
   toSaveMap.put(key, value);
  }
  //2.增加你需要增加的属性内容
  toSaveMap.put("name", "zhang san");
  toSaveMap.put("age", "25");
  prop.putAll(toSaveMap);
  prop.store(out, "==== after add ====");
  
  /**
   * 修改逻辑:重新设置对应Key的值即可,非常简单
   */
  prop.clear();
  toSaveMap.put("name", "li si");
  toSaveMap.put("age", "26");
  prop.putAll(toSaveMap);
  prop.store(out, "==== after modify ====");
  
  /**
   * 删除逻辑:找到对应的key,删除即可
   */  
  prop.clear();
  toSaveMap.remove("name");
  prop.putAll(toSaveMap);
  prop.store(out, "==== after remove ===="));
  
  /**
   * 查询逻辑:
   */
  InputStream in = new FileInputStream("D:\\workspace\\JavaStudy\\src\\test\\test.properties");
  prop.load(in);
  System.out.println("name: " + prop.get("name"));
  System.out.println("age: " + prop.get("age"));
  
 }