Java中对Properties的操作
如果,项目中对properties文件的CURD操作比较多,建议建个工具类,方便调用。如下:
package com.properties; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; public class OperatePropertiesUtil{ //加载、读取Properties文件 public static Properties loadProperties(String propertiesPath){ Properties pps = new Properties(); try { InputStream in = new FileInputStream(propertiesPath); pps.load(in); in.close(); }catch (IOException e) { e.printStackTrace(); } return pps; } //向Properties文件中加入数据 public static Properties addProperties(String propertiesPath,Properties pps,String key,String value){ try { OutputStream fos = new FileOutputStream(propertiesPath); pps.setProperty(key, value); pps.store(fos, "Update '" + key + "' value"); fos.close(); }catch (IOException e) { e.printStackTrace(); } return pps; } //从Properties文件中根据key删除 public static Properties removeProperties(String propertiesPath,Properties pps,String key){ try { OutputStream oFile = new FileOutputStream(propertiesPath); pps.remove(key); pps.store(oFile, "Delete '" + key); oFile.close(); }catch (IOException e) { e.printStackTrace(); } return pps; } //Properties文件中根据key更新value public static Properties updatePropertiesByKey(String propertiesPath,Properties pps,String key,String value){ try { OutputStream oFile = new FileOutputStream(propertiesPath); pps.setProperty(key, value); pps.store(oFile, "Update '" + key); oFile.close(); }catch (IOException e) { e.printStackTrace(); } return pps; } //update Properties文件,以byte[]的形式,重新向properties中写入,特殊情况用 public static Properties updateProperties(String propertiesPath,Properties pps,byte [] str){ try { File file = new File(propertiesPath); FileOutputStream oFile = new FileOutputStream(file); oFile.write(str); oFile.close(); }catch (IOException e) { e.printStackTrace(); } return pps; } }
如果是javaweb项目,文件的路径只能取到已经部署到的服务下的webapp下的文件路径。
比如这样:String path = request.getServletContext().getRealPath("/");
String realPath = path+"WEB-INF\\classes"+"文件名.后缀名";
或者其他的取路径方法也可。
下面是调用方法,这些可以自己在项目中根据情况具体封装。
package com.properties; import java.util.Properties; import java.util.Set; public class TestCurd { public static void main(String[] args) { String propertiesPath = "你的文件路径"; Properties pps = OperatePropertiesUtil.loadProperties(propertiesPath); //showPro(pps); //showPro(addPro(propertiesPath, pps)); //showPro(removePro(propertiesPath, pps)); //showPro(updateProByKey(propertiesPath, pps)); //showPro(updatePro(propertiesPath, pps)); } //遍历显示properties public static void showPro(Properties pps){ Set<Object> set = pps.keySet(); for (Object object : set) { System.out.println("key:"+(String)object+"\t"+"value:"+pps.getProperty((String)object)); } } //向properties添加数据 public static Properties addPro(String propertiesPath,Properties pps){ Properties ppss = OperatePropertiesUtil.addProperties(propertiesPath, pps,"addkey","addvalue"); return ppss; } //向properties删除数据 public static Properties removePro(String propertiesPath,Properties pps){ Properties ppss = OperatePropertiesUtil.removeProperties(propertiesPath, pps,"addkey"); return ppss; } //更新properties数据 public static Properties updateProByKey(String propertiesPath,Properties pps){ Properties ppss = OperatePropertiesUtil.updatePropertiesByKey(propertiesPath, pps,"addkey","updateValue"); return ppss; } //以byte[]的形式重新写入properties数据 //javaweb项目中,只能操作webapp下的properties文件,每次操作并不会覆盖原始版本,此时可用此方法。 public static Properties updatePro(String propertiesPath,Properties pps){ Set<Object> sett = pps.keySet(); StringBuffer strbu = new StringBuffer(""); for (Object object : sett) { if("addkey".equals((String)object)){ strbu = strbu.append((String)object+"="+"uValue2"+"\n"); }else{ String res = pps.getProperty((String)object); strbu = strbu.append((String)object+"="+res+"\n"); } } byte [] str = strbu.toString().getBytes(); Properties ppss = OperatePropertiesUtil.updateProperties(propertiesPath,pps,str); return ppss; } }