property文件读取工具类
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PropertiesUtils {
private static final Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);
private static Properties properties = null;
private static final String DEFAULT_PATH = "/config.properties";
private PropertiesUtils() {
}
public static String getConfig(String key) {
return properties.getProperty(key);
}
public static String getConfig(String key, String defaultValue) {
String value = properties.getProperty(key);
return StringUtils.isEmpty(value)?defaultValue:value;
}
public static Properties getProperties() {
return properties;
}
public static String getProperty(String key, String path) {
Properties propertie = new Properties();
InputStream in = PropertiesUtils.class.getResourceAsStream(path);
try {
propertie.load(in);
} catch (IOException var5) {
logger.error(var5.getMessage(), var5);
return null;
}
return propertie.getProperty(key);
}
public static String getProperty(String key, String defaultValue, String path) {
String value = getProperty(key, path);
return StringUtils.isEmpty(value)?defaultValue:value;
}
static {
try {
properties = new Properties();
InputStream e = PropertiesUtils.class.getResourceAsStream("/config.properties");
properties.load(e);
} catch (IOException var1) {
logger.error(var1.getMessage(), var1);
}
}
}