properties文件装载读取Util
2016-06-19 16:03
190 查看
package com.tianmushanlu.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
/**
* properties文件装载读取类
*
*
*
*/
public class Config {
private static final Log log = LogFactory.getLog(Config.class);
private static final String CONFIG_FILE = "classpath*:conf/*.properties";
private static final Config instance = new Config();
private final Map<String, String> configCache = new HashMap<String, String>();
/**
* 默认装载conf目录下的所有以.properties为后缀的属性配置文件
*/
private Config() {
Resource[] resources = getResources(CONFIG_FILE);//加载所有文件
for (Resource resource : resources) {
InputStream in = null;
try {
Properties properties = new Properties();
in = resource.getInputStream();
properties.load(in);
Enumeration<?> propertyNames = properties.propertyNames();
//读取各文件中的属性名
while (propertyNames != null && propertyNames.hasMoreElements()) {
String name = (String) propertyNames.nextElement();
if (StringUtils.isNotEmpty(name)) {
//将各文件中的属性名添加至configCache,以键值对的形式存储
configCache.put(name, properties.getProperty(name));
}
}
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("解析配置文件[classpath:" + resource.getFilename() + "]异常:", e);
}
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(in);
}
}
}
public static Config getInstance() {
return instance;
}
public String getString(String key) {
return configCache.get(key);
}
/**
* 在classpath*目录下搜索资源文件
*
* @param locationPattern
* @return
*/
public static Resource[] getResources(String locationPattern) {
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = new Resource[0];
try {
resources = patternResolver.getResources(locationPattern);
} catch (IOException e) {
throw new RuntimeException(e);
}
return resources;
}
}
相关文章推荐
- 如何利用spring和java.util.propeties读取properties属性文件里的值
- java.util.Properties读取中文内容的配置文件,发生中文乱码的现象有解决方案
- 通过java.util.Properties类来读取.properties文件中key对应的value
- 读取资源属性文件(properties)java.util.ResourceBundle使用详解
- Java读取配置文件 java.util.Properties的使用
- 使用java.util.Properties读取文件
- java.util.Properties 读取资源文件
- java.util.ResourceBundle读取properties文件
- java.util.ResourceBundle 和java.util.properties 读取配置文件区别
- java读取配置文件(使用java.util.Properties读取)
- java使用java.util.Properties读取properties文件的九种方法
- J2SE API读取Properties文件六种方法(装载)
- properties文件的读取Util
- 使用java.util.Properties读取配置文件信息
- java.util.ResourceBundle 和java.util.properties 读取配置文件区别
- Eclipse和Tomcat和Java读取配置文件工具类PropertiesUtil
- 利用java.util.Properties读取属性文件
- Spring中配置文件读取多个Properties文件
- Spring读取properties文件出现乱码的解决方法
- Java 读取配置文件Properties