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:");
}
}