import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PropertieUtils {
private final static Logger log = LoggerFactory.getLogger(PropertieUtils.class);
private Properties properties = new Properties();
public PropertieUtils(){
this.initProperties();
}
private void initProperties(){
InputStream rs = null;
String appPath = "";
try{
//如果jar包外有配置文件就以jar外的配置为准,否则读取jar包内的配置文件
appPath = System.getProperty("user.dir");
//appPath = CommUtils.getAppPath();
File file = new File(appPath + File.separator + "xetl-message.properties");
if (file.exists()){
rs = new FileInputStream(file);
}else{
rs = Thread.currentThread().getContextClassLoader().getResourceAsStream("xetl-message.properties");
}
properties.load(rs);
}catch(Exception e){
log.error("读取xetl-message.properties配置文件失败," + appPath + "," + e.getMessage(), e);
//throw new RuntimeException("读取xetl-message.properties配置文件失败" + e.getMessage() + "," + appPath);
}finally{
try{
if (rs != null){
rs.close();
}
}catch(Exception e){
}
}
}
public String getConfigValue(String key)
{
return properties.getProperty(key);
}
public int getIntValue(String key)
{
String value = getConfigValue(key);
return Integer.parseInt(value);
}
public String getDefaultValue(String key, String value)
{
String result = properties.getProperty(key);
if (result == null || result.length() == 0)
{
return value;
}
return result;
}
}
main (){
PropertieUtils properties = new PropertieUtils("xetl-sun.properties");
}