首页 > 学院 > 开发设计 > 正文

常用工具类

2019-11-08 18:48:42
字体:
来源:转载
供稿:网友

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); } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表