欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java properties增删改查,properties增删,java propert

来源: javaer 分享于  点击 28446 次 点评:167

java properties增删改查,properties增删,java propert


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")); }
相关栏目:

用户点评