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. ...
随机推荐
- SQL SERVER数据库维护与重建索引
第一步:查看是否需要维护,查看扫描密度/Scan Density是否为100% declare @table_id int set @table_id=object_id('表名') dbcc sho ...
- 转 Java笔记:Java内存模型
Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...
- 常用的代码之一:用StopWatch计算代码运行花费的时间。
先引用Diagnostics using System.Diagnostics; 然后: Stopwatch stopWatch = new Stopwatch(); stopWatch.Start( ...
- 中控考勤机SDK使用中员工姓名的处理( c# )
公司使用的考勤机是中控的指纹考勤机,但是中控的型号乱七八糟,通过程序读出来的型号和实际标的型号不一致. 另外,提供的开发包的C#版本的Demo中调用 axCZKEM1.ReadAllUserID(iM ...
- 微信小程序之顶部固定和底部固定
顶部固定 <view style="position:fixed;top:0;"> ...... </view> 底部固定 <view style=& ...
- Hadoop创建/删除文件夹出错
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFact ...
- Ubuntu中开启和关闭防火墙-摘自网络
1.关闭ubuntu的防火墙 ufw disable开启防火墙ufw enable 2.卸载了iptablesapt-get remove iptables3.关闭ubuntu中的防火墙的其余命令ip ...
- css冻结列的效果
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="utf-8&quo ...
- Atitit spring 3.0 3.1 3.2 4.0 4.3 5.0 新特性
Atitit spring 3.0 3.1 3.2 4.0 4.3 5.0 新特性 Spring3.0的新特性及其分析 - 我的人生不甘于平庸! - ITeye技术网站.html Spring3.0带 ...
- [sql]mysql5.6cmake安装/mysql5.7二进制安装
centos7上cmake编译安装mysql-5.6.36.tar.gz 系统环境 - 环境(安装前规划好主机名,mysql编译过程会用) [root@n1 mysql-5.6.36]# cat /e ...