package com.hxkj.fsslr.core.utils;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/ * * *@ClassName: PropertiesUtils
* @Description: configuration file reading utility class *@author [email protected]
* @dateJuly 14, 2016 6:00:34 PM */
public class PropertiesUtils {
private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);
/** * Default configuration file */
private static final String CONFIG_FILE_PATH = "config.properties";
/** * Read the object in the properties file */
private static Properties prop = new Properties();
static{
reflushProperties(CONFIG_FILE_PATH);
}
/ * * *@Title: reflushProperties
* @Description: Refreshes configuration file configuration *@param configFileName
* @return void
* @author [email protected]
* @dateJuly 14, 2016 6:00:01 PM */
public static void reflushProperties(String configFileName){
String path = "";
if(configFileName ! =null&& configFileName.trim() ! =""){
path = PropertiesUtils.class.getClassLoader().getResource(configFileName).getPath();
}else{
path = PropertiesUtils.class.getClassLoader().getResource("config.properties").getPath();
}
try{
FileInputStream in = new FileInputStream(path);
prop.load(in);
in.close();
}catch(Exception e){ e.printStackTrace(); }}/ * * *@Title: getPropertiesValue
* @Description: Gets the value * by attribute name@param propertiesName
* @return
* @return String
* @author [email protected]
* @dateJuly 14, 2016 5:59:42 PM */
public static String getPropertiesValue(String propertiesName){
return prop.getProperty(propertiesName);
}
/ * * *@Title: setPropertiesValue
* @Description: Updates or adds attributes *@param propertiesName
* @param propertiesValue
* @return
* @return boolean
* @author [email protected]
* @dateJuly 21, 2016 4:02:15 PM */
public static boolean setPropertiesValue(String propertiesName,String propertiesValue){
boolean result = false;
String path = PropertiesUtils.class.getClassLoader().getResource(CONFIG_FILE_PATH).getPath();
try {
FileOutputStream out = new FileOutputStream(path);
prop.setProperty(propertiesName, propertiesValue);
prop.store(out, "update CUID_PRE");
out.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
logger.error("Fsslr-webserver-error :config.properties write failed");
}
returnresult; }}Copy the code