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. ...
随机推荐
- 机器学习---支持向量机(SVM)
非常久之前就学了SVM,总认为不就是找到中间那条线嘛,但有些地方模棱两可,真正编程的时候又是一团浆糊.參数任意试验,毫无章法.既然又又一次学到了这一章节,那就要把之前没有搞懂的地方都整明确,嗯~ 下面 ...
- 全面拥抱移动测试,Mobile JSON Wire Protocol Specification文档翻译
Selenium3已经宣布不支持移动化测试.对于老牌测试工具selenium来说这是以退为进,因为移动自动化测试工具的标准还在selenium团队手上. 本文轻度翻译了这个标准,看得懂的人不用翻译也能 ...
- WinForm DataGridView新增加行
1.不显示最下面的新行 通常 DataGridView 的最下面一行是用户新追加的行(行头显示 * ).如果不想让用户新追加行即不想显示该新行,可以将 DataGridView 对象的 Allow ...
- 【转载并整理】JAVA解析或生成xml的四种方法
参考文章 1:http://blog.csdn.net/clemontine/article/details/53011362 2:http://www.jb51.net/article/98456. ...
- Java 8 Stream – Read a file line by line
In Java 8, you can use Files.lines to read file as Stream. c://lines.txt – A simple text file for te ...
- React Native 错误锦集
启动时报错 : React Native version mismatch. JavaScript version: 0.57.4 Native version: 0.55.2 解决方案传送门:htt ...
- python中decode和encode的区别
#-*-coding:utf-8 import sys ''' *首先要搞清楚,字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码, 即先将 ...
- spring中InitializingBean接口使用理解(转)
InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...
- flowable 中task的相关操作
1 获取任务列表 1)获取候选人的任务列表 TaskService taskService = processEngine.getTaskService(); List<Task> tas ...
- django session入门详解
概括性的讲: 1.django默认是打开对session的支持的 2.默认情况下session相关的数据会保存在数据库中.浏览器端只保存了session id session 的科普: 1.动态网站中 ...