package com.suning.ebuy.zone.review.utils; import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; import com.suning.framework.lang2.SnfLogger; import com.suning.framework.lang2.SnfLoggerFactory; import com.suning.framework.lang2.dto.EventMessage; public class GetProperties { private static final SnfLogger logger = SnfLoggerFactory.getLogger(GetProperties.class); private static Properties properties = new Properties(); public static Properties getProperties() { return properties; } public static void setProperties(Properties properties) { GetProperties.properties = properties; } //利用靜態代碼塊初始化數據 static{ InputStream in = GetProperties.class.getResourceAsStream("/conf/orderShow.properties"); if(in==null){ logger.info("importLog.properties not found"); }else{ if(!(in instanceof BufferedInputStream)){ in = new BufferedInputStream(in); } try{ properties.load(in); in.close(); }catch (Exception e) { logger.info("Error while processing importLog.properties"); } } } /** * 向properties文件中寫入數據 */ public static void writeProperties(String parameterName, String parameterValue) { logger.info("enter:GetProperties類中的方法:writeProperties:"); OutputStream fos = null; try { fos = new FileOutputStream("/conf/orderShow.properties"); properties.setProperty(parameterName, parameterValue); // 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流 properties.store(fos, "『comments』Update key:" + parameterName); } catch (IOException e) { EventMessage eventMessage = new EventMessage(); eventMessage.setFunctionName(ExceptionUtil.getCurrentMethodName()); logger.logException(eventMessage, e); }finally{ if(null != fos){ try { fos.close(); } catch (IOException e) { logger.warn("IO ERROR:" + e.getMessage()); } } } logger.info("exit:GetProperties類中的方法:writeProperties:"); } }