java读取配置文件内容
利用com.typesafe.config包实现
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.0.2</version>
</dependency>
被读取的配置文件config.properties:
patrol.interval=5
push.interval=30
data = [{ controlNum : 1, loopNum : 1, pointNum : 1, status : 110 },\
{ controlNum : 1, loopNum : 1, pointNum : 1, status = 111 }]
java 工具类:
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory; public class ServerConfig { private final static Config config; static {
config = ConfigFactory.load("config.properties");
} public static Config load() {
return config;
}
}
调用方式:
Config serverConfig = ServerConfig.load();
int PATROL_INTERVAL = serverConfig.getInt("patrol.interval");
或者使用Java IO实现
public class PropertiesReader { private static final String PROPERTIES_FILE_NAME = "aa.properties"; private static Properties config = null; static {
config = readProperties(PROPERTIES_FILE_NAME);
} public static String getProperty(String key){
return config.getProperty(key);
} private static Properties readProperties(String fileName){
InputStream inputStream = WsPropertiesReader.class.getClassLoader().getResourceAsStream(fileName);
Properties config = new Properties();
try {
config.load(inputStream);
} catch(IOException e){
e.printStackTrace();
} finally{
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return config;
} public static Properties readProperties(InputStream inputStream){
Properties config = new Properties();
try {
config.load(inputStream);
} catch(IOException e){
e.printStackTrace();
} finally{
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return config;
}
}
对于配置文件中有嵌套的配置内容,一种方法是,在Spring环境中
使用EmbeddedValueResolverAware读取配置文件内容
另外可以自己解析${}的形式
public class PropertiesReader { private static final String COMMON_PROPERTIES_FILE_NAME = "common-constants.properties";
private static final String WEB_PROPERTIES_FILE_NAME = "application.properties"; private static Properties commonConfig = null;
private static Properties webConfig = null; static {
commonConfig = readProperties(COMMON_PROPERTIES_FILE_NAME);
webConfig = readProperties(WEB_PROPERTIES_FILE_NAME);
} public static String getProperty(String key) {
return commonConfig.getProperty(key);
} /**
* 支持复杂el表达式递归调用
* @param key
* @return
*/
public static String getWebProperty(String key) {
String value = webConfig.getProperty(key);
String regex = "\\$\\{(.*?)\\}"; //正则表达式
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
while(matcher.find()){
String subProperty = getWebProperty(matcher.group(1));
value = value.replace("${"+matcher.group(1)+"}",subProperty);
}
return value;
} private static Properties readProperties(String fileName) {
InputStream inputStream = PropertiesReader.class.getClassLoader().getResourceAsStream(fileName); Properties config = new Properties();
try {
config.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return config;
}
}
java读取配置文件内容的更多相关文章
- java读取配置文件的几种方法
java读取配置文件的几种方法 原文地址:http://hbcui1984.iteye.com/blog/56496 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配 ...
- Python+selenium之读取配置文件内容
Python+selenium之读取配置文件内容 Python支持很多配置文件的读写,此例子中介绍一种配置文件的读取数据,叫ini文件,python中有一个类ConfigParser支持读ini文件. ...
- Java读取配置文件的方式
Java读取配置文件的方式-笔记 1 取当前启动文件夹下的配置文件 一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...
- java读取文本文件内容2
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/183 很久之前写了一篇Java读取文本文件内容,链接地址是 ...
- java读取文本文件内容
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/128 java读取文本文件内容 今天写代码写着要调试一个很 ...
- java读取word内容
暂时只写读取word内容的方法. 依赖的jar: poi-3.9-20121203.jarpoi-ooxml-3.9-20121203.jarxmlbeans-2.3.0.jar package co ...
- java读取配置文件
java 读取文件可以用字节流和字符流. 由于一个汉字占两个字节,所以如果配置文件中有汉字,用字节流读取,会出现乱码. 用字符流则不会出现乱码. 配置文件 b.properties 文件如下: fam ...
- Java 读取配置文件数据
Properties类 Properties类,是一个工具类,包含在java.util包中. 功能:可以保存持久的属性,通常用来读取配置文件或者属性文件,将文件中的数据读入properties对象中, ...
- java读取配置文件方法以及工具类
第一种方式 : java工具类读取配置文件工具类 只是案例代码 抓取异常以后的代码自己处理 import java.io.FileNotFoundException; import java.io. ...
随机推荐
- samba 服务器搭建
为了能在两台机器上共享代码,方便测试不同平台性能和搭建分布式的web server,今天耗费半天时间搭建一个samba服务器共享数据,要求开放写权限,但多次实验均告失败,最终在 鸟哥 的提醒下 检查发 ...
- Openssl aes加解密例程 更进一步
原文链接: http://blog.csdn.net/itmes/article/details/7718427 前面我们用openssl的aes256对称加密算法对16个字节的内存块进行了的加解密运 ...
- SharePoint 2013 创建Web Application
今天继续SharePoint 2013 的探索之旅,之前几篇文章分析了SharePoint 2013的物理拓扑结构,安装,以及逻辑体系结构.在这篇文章中,我将继续Step By Step形式演示如何在 ...
- debian 8 解压安装mysql(版本5.7.19)
debian 8 解压安装mysql(版本5.7.19)一.下载 根据目标主机的型号官网下载mysql安装包如: mysql-server_5.7.19-1debian8_amd64.deb-bund ...
- 转 MySQL问题排查工具介绍
原文链接: http://mrchenatu.com/2017/03/24/mysql-tool/ 本总结来自美团内部分享,屏蔽了内部数据与工具 知识准备 索引 索引是存储引擎用于快速找到记录的一种数 ...
- Android Logcat信息级别解读
Android Logcat信息级别解读 Logcat信息分为好几个级别,分别是:Assert(断言).Debug(调试).Error(错误).Info(信息).Verbose(详细).Warning ...
- php分享二十四:数组
1:isset() 对于数组中为 NULL 的值不会返回 TRUE,而 array_key_exists() 会. 2:利用array_filter和strlen快速过滤数组中等于0的值 $path ...
- Oozie4.2 安装部署、以及example测试
编译: 使用的环境是:Hadoop2.6.0.Spark1.4.0.Hive0.13.1.Sqoop1.4.4 编译Oozie的命令:./mkdistro.sh -Phadoop-2 -Dhadoop ...
- JQuery设置获取下拉菜单选项的值 多实例
分享下JQuery如何设置获取下拉菜单某个选项的值,多种方法,值得收藏. JQuery获取和设置Select选项 获取Select :获取select 选中的 text :$(“#ddlRegType ...
- 【Unity】第7章 输入控制
分类:Unity.C#.VS2015 创建日期:2016-04-21 一.简介 Unity提供了-个非常易用和强大的用于处理输入信息的类:Input,利用该类可以处理鼠标.键盘.摇杆/方向盘/手柄等游 ...