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;
}
}
分享到:
相关推荐
在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。比如说我们开发了一个操作数据库的模块,在开发的时候我们...JDK 内置的java.util.Properties 类为我们操作.properties 文件提供了便利。
使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); 2。使用java.util....
ava中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置...在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,文本注释信息可以用"#"来注释。
API读取Properties文件的六种方法2009-03-02 08:57使用J2SE API读取Properties文件的六种方法 1。使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream...
java中用FileUtil实现文件读写
FTP文件上传下载Util类
这是一个简单实现读取properties、xml格式的配置文件的小案例。虽然实际项目中可能不是这样实现的。... 方法二:java.util.Properties读取properties类型文件; 二、读取xml类型文件 dom4j读取xml文件;
NULL 博文链接:https://liuzidong.iteye.com/blog/776637
而且在java 中有 java.util.Properties 这个类,可以很方便的处理properties 文件, 在python 中虽然也有读取配置文件的类ConfigParser, 但如果习惯java 编程的人估计更喜欢下面这个用python 实现的读取 properties ...
context:property-placeholder 和util:properties 博客:https://blog.csdn.net/u010476739/article/details/76735527
import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ReadProperties { private static final Log log = LogFactory.getLog...
SpringBoot2.1.0与Dubbo2.6.5基于properties文件的整合--Maven项目工程
extjs实例 入门,提供ext所需要的资源文件,详细叙述怎么改变文件颜色
Android读取properties配置文件的实例详解 因为一些配置信息,多处用到的。且以后可能变更的,我想写个.prorperties配置文件给管理起来。 我把配置文件放在了assets文件夹下 appConfig.properties: serverUrl=...
JDK 内置的 Java.util.Properties 类为我们操作 .properties 文件提供了便利。 一. .properties 文件的形式 # 以下为服务器、数据库信息 dbPort = localhost databaseName = mydb dbUserName = root ...
不是API明智的(API完全不同),但是能够读取每个Java .properties文件并获得与java.util.Properties完全相同的键值对。 但是,Apron会维护属性文件中条目的顺序,以及键,分隔符之前的注释,空白行和空格。 这样...
使用util.js工具类可以在前端页面直接获取地址栏的参数的值
java 使用 JAXB 将xml转换为 bean 包含xml和dto和读取文件的util类
PropertiesUtil实现对properties文件的读与写,参数文件名不包括.properties SysConfig实现对指定系统资源文件(config/sysConfig.properties)的读取操作,利用修改时间判断是否重新加载文件 DateUtil提供了很多...
log4j.properties ,commons-logging-1.2.jar,log4j-1.2.16.jar,三个文件一起组成一个完整的日志输出,Apache的Common Logging只是一个高层的日志框架,本身并没有实现真正的写日志能力,而是依赖其它的日志系统如...