清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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" )); } |