利用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读取配置文件内容的更多相关文章

  1. java读取配置文件的几种方法

    java读取配置文件的几种方法 原文地址:http://hbcui1984.iteye.com/blog/56496         在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配 ...

  2. Python+selenium之读取配置文件内容

    Python+selenium之读取配置文件内容 Python支持很多配置文件的读写,此例子中介绍一种配置文件的读取数据,叫ini文件,python中有一个类ConfigParser支持读ini文件. ...

  3. Java读取配置文件的方式

    Java读取配置文件的方式-笔记 1       取当前启动文件夹下的配置文件   一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...

  4. java读取文本文件内容2

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/183 很久之前写了一篇Java读取文本文件内容,链接地址是 ...

  5. java读取文本文件内容

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/128 java读取文本文件内容 今天写代码写着要调试一个很 ...

  6. java读取word内容

    暂时只写读取word内容的方法. 依赖的jar: poi-3.9-20121203.jarpoi-ooxml-3.9-20121203.jarxmlbeans-2.3.0.jar package co ...

  7. java读取配置文件

    java 读取文件可以用字节流和字符流. 由于一个汉字占两个字节,所以如果配置文件中有汉字,用字节流读取,会出现乱码. 用字符流则不会出现乱码. 配置文件 b.properties 文件如下: fam ...

  8. Java 读取配置文件数据

    Properties类 Properties类,是一个工具类,包含在java.util包中. 功能:可以保存持久的属性,通常用来读取配置文件或者属性文件,将文件中的数据读入properties对象中, ...

  9. java读取配置文件方法以及工具类

    第一种方式 : java工具类读取配置文件工具类 只是案例代码  抓取异常以后的代码自己处理 import java.io.FileNotFoundException; import java.io. ...

随机推荐

  1. C#中委托、事件和回调函数的理解

    在C#中我们经常会碰到事件,尤其是在WPF或者WinForm中,窗体加载.或者点击一个按钮,都会触发事件.实际上,事件是对委托的封装.如果不进行封装,让委托暴露给调用者,调用者就可以把委托变量重新引用 ...

  2. 基于Android平台的会议室管理系统具体设计说明书

    会议室管理系统具体设计说明书 第一部分  引言 1.编写目的 本说明对会议室管理系统项目的各模块.页面.脚本分别进行了实现层面上的要求和说明. 软件开发小组的产品实现成员应该阅读和參考本说明进行代码的 ...

  3. 安装CDH5时出错 5.68.168.192.in-addr.arpa domain name pointer bogon.

    这个是因为使用的DNS是联通的server,联通会将所有保留的网络地址RFC1918( http://tools.ietf.org/html/rfc1918)都指向"bogon" ...

  4. MySQL开启federated引擎实现数据库表映射

    1.查看federated引擎是否开启 点击进入Navicat并点击键盘上F6,出现命令行界面 ,输入指令:show engines; 2.开启federated引擎 Windows系统 : 在my. ...

  5. CentOS 安装Mosquitto及测试

    系统信息,阿里云服务器 安装工具 yum install gcc gcc-c++ yum install openssl-devel yum install c-ares-devel yum inst ...

  6. 如何设置Linux swap分区

    看到不少朋友问linux下swap分区的问题,收集到一篇 不错的文章 ,分享下.什么是Swap?Swap,即交换区,除了安装Linux的时候,有多少人关心过它呢?其实,Swap的调整对Linux服务器 ...

  7. 2. Apache Axis2 快速学习手册之概览

    这篇博文和大家一起学习下Apache Axis2 官方文档的快速指南篇 英文原文:http://axis.apache.org/axis2/java/core/docs/quickstartguide ...

  8. DBeaver利用方式简介

    当攻击者能够进入内网,同时发现某台服务器装有弱口令的数据库时,除了在console中查看数据库信息外, 还可以利用第三方工具进行数据库读取,本例中使用DBeaver进行查看. 1. 下载DBeaver ...

  9. [Windows Azure] Windows Azure SQL Database library

    Microsoft Windows Azure SQL Database extends SQL Server capabilities to the cloud. SQL Database offe ...

  10. tornado设置settings

    1.作用 设置应用程序相关参数 2.用法 settings = dict() settings["debug"] = True tornado.web.Application.__ ...