1.讀取配置文件的鍵值對,轉爲Properties對象;將Properties(鍵值對)對象寫入到指定文件。java
package com.ricoh.rapp.ezcx.admintoolweb.util; 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; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class PropertiesFileHandle { private static Logger logger = LoggerFactory.getLogger(PropertiesFileHandle.class); public static Properties readProperties(String filePath) { String realPath = FileUtil.getEzChargerInstallPath() + filePath; Properties props = new Properties(); File configFile = new File(realPath); logger.debug("#configFile: " + configFile.getAbsolutePath()); InputStream fis = null; try { fis = new FileInputStream(configFile); props.load(fis); } catch (IOException e) { logger.error("readProperties failed in" + realPath + ". "+ e.toString()); return null; } finally { try { if (fis != null) { fis.close(); } } catch (Exception e) { logger.debug("readProperties close file failed." + e.toString()); } } return props; } public static boolean writeProperties(String filePath, Properties prop) { String realPath = FileUtil.getEzChargerInstallPath() + filePath; File configFile = new File(realPath); if(!configFile.exists()) { configFile.getParentFile().mkdirs(); try { configFile.createNewFile(); } catch (IOException e) { logger.error("PropertiesFileHandle.writeProperties failed. because create file[" + realPath + "]. is IOException:"+ e.getMessage()); e.printStackTrace(); return false; } } InputStream fis = null; OutputStream fos = null; try { fos = new FileOutputStream(configFile); prop.store(fos, ""); } catch (Exception e) { logger.error("WriteProperties failed in" + realPath + ". "+ e.toString()); return false; } finally { try { if (fos != null) { fos.close(); } if (fis != null) { fis.close(); } } catch (Exception e) { logger.debug("writeProperties close file failed." + e.toString()); } } return true; } }
2.經過輸入流或者Properties對象將Properties文件的內容讀取到map集合中。web
package com.ricoh.rapp.ezcx.edcactivation.internal; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Map.Entry; public class PropertyUtil { public static Map<String, String> loadPropertiesFile(InputStream file) { HashMap result = new HashMap(); try { Properties prop = new Properties(); prop.load(file); Iterator var4 = prop.entrySet().iterator(); while (var4.hasNext()) { Entry<Object, Object> entry = (Entry) var4.next(); result.put((String) entry.getKey(), (String) entry.getValue()); } } catch (IOException var5) { System.out.println("faild load properties file ."); } return result; } public static Map<String, String> loadPropertiesFile(Properties prop) { Map<String, String> result = new HashMap(); if (prop == null) { return result; } else { Iterator var5 = prop.entrySet().iterator(); while (var5.hasNext()) { Entry<Object, Object> entry = (Entry) var5.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); if (key != null && key.length() > 0 && value != null && value.length() > 0) { result.put(key, value); } } return result; } } }
3.實例使用1和2的方式來處理Properties文件app
private void innitPropreties() { Map<String, String> rsiConfMap = new HashMap<>(); Properties proxyProp = PropertiesFileHandle.readProperties("/conf/test.properties"); if (proxyProp != null) { rsiConfMap = PropertyUtil.loadPropertiesFile(proxyProp); }else { rsiConfMap.put("key1", "value1"); rsiConfMap.put("key2", "value2"); Properties properties = new Properties(); properties.put("key1", "value1"); properties.put("key2", "value2"); PropertiesFileHandle.writeProperties("/conf/test.properties", properties); } String value1= rsiConfMap.get("key1"); String value2= rsiConfMap.get("key2"); }