利用com.typesafe.config包实现

  1. <dependency>
  2. <groupId>com.typesafe</groupId>
  3. <artifactId>config</artifactId>
  4. <version>1.0.2</version>
  5. </dependency>

被读取的配置文件config.properties:

  1. patrol.interval=5
  2. push.interval=30
  3. data = [{ controlNum : 1, loopNum : 1, pointNum : 1, status : 110 },\
  4. { controlNum : 1, loopNum : 1, pointNum : 1, status = 111 }]

java 工具类:

  1. import com.typesafe.config.Config;
  2. import com.typesafe.config.ConfigFactory;
  3.  
  4. public class ServerConfig {
  5.  
  6. private final static Config config;
  7.  
  8. static {
  9. config = ConfigFactory.load("config.properties");
  10. }
  11.  
  12. public static Config load() {
  13. return config;
  14. }
  15. }

调用方式:

  1. Config serverConfig = ServerConfig.load();
  2. int PATROL_INTERVAL = serverConfig.getInt("patrol.interval");

或者使用Java IO实现

  1. public class PropertiesReader {
  2.  
  3. private static final String PROPERTIES_FILE_NAME = "aa.properties";
  4.  
  5. private static Properties config = null;
  6.  
  7. static {
  8. config = readProperties(PROPERTIES_FILE_NAME);
  9. }
  10.  
  11. public static String getProperty(String key){
  12. return config.getProperty(key);
  13. }
  14.  
  15. private static Properties readProperties(String fileName){
  16. InputStream inputStream = WsPropertiesReader.class.getClassLoader().getResourceAsStream(fileName);
  17. Properties config = new Properties();
  18. try {
  19. config.load(inputStream);
  20. } catch(IOException e){
  21. e.printStackTrace();
  22. } finally{
  23. if (inputStream != null){
  24. try {
  25. inputStream.close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31.  
  32. return config;
  33. }
  34.  
  35. public static Properties readProperties(InputStream inputStream){
  36. Properties config = new Properties();
  37. try {
  38. config.load(inputStream);
  39. } catch(IOException e){
  40. e.printStackTrace();
  41. } finally{
  42. if (inputStream != null){
  43. try {
  44. inputStream.close();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50.  
  51. return config;
  52. }
  53. }

对于配置文件中有嵌套的配置内容,一种方法是,在Spring环境中

使用EmbeddedValueResolverAware读取配置文件内容

另外可以自己解析${}的形式

  1. public class PropertiesReader {
  2.  
  3. private static final String COMMON_PROPERTIES_FILE_NAME = "common-constants.properties";
  4. private static final String WEB_PROPERTIES_FILE_NAME = "application.properties";
  5.  
  6. private static Properties commonConfig = null;
  7. private static Properties webConfig = null;
  8.  
  9. static {
  10. commonConfig = readProperties(COMMON_PROPERTIES_FILE_NAME);
  11. webConfig = readProperties(WEB_PROPERTIES_FILE_NAME);
  12. }
  13.  
  14. public static String getProperty(String key) {
  15. return commonConfig.getProperty(key);
  16. }
  17.  
  18. /**
  19. * 支持复杂el表达式递归调用
  20. * @param key
  21. * @return
  22. */
  23. public static String getWebProperty(String key) {
  24. String value = webConfig.getProperty(key);
  25. String regex = "\\$\\{(.*?)\\}"; //正则表达式
  26. Pattern pattern = Pattern.compile(regex);
  27. Matcher matcher = pattern.matcher(value);
  28. while(matcher.find()){
  29. String subProperty = getWebProperty(matcher.group(1));
  30. value = value.replace("${"+matcher.group(1)+"}",subProperty);
  31. }
  32. return value;
  33. }
  34.  
  35. private static Properties readProperties(String fileName) {
  36. InputStream inputStream = PropertiesReader.class.getClassLoader().getResourceAsStream(fileName);
  37.  
  38. Properties config = new Properties();
  39. try {
  40. config.load(inputStream);
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. } finally {
  44. if (inputStream != null) {
  45. try {
  46. inputStream.close();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52.  
  53. return config;
  54. }
  55. }

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. SQL SERVER数据库维护与重建索引

    第一步:查看是否需要维护,查看扫描密度/Scan Density是否为100% declare @table_id int set @table_id=object_id('表名') dbcc sho ...

  2. 转 Java笔记:Java内存模型

    Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...

  3. 常用的代码之一:用StopWatch计算代码运行花费的时间。

    先引用Diagnostics using System.Diagnostics; 然后: Stopwatch stopWatch = new Stopwatch(); stopWatch.Start( ...

  4. 中控考勤机SDK使用中员工姓名的处理( c# )

    公司使用的考勤机是中控的指纹考勤机,但是中控的型号乱七八糟,通过程序读出来的型号和实际标的型号不一致. 另外,提供的开发包的C#版本的Demo中调用 axCZKEM1.ReadAllUserID(iM ...

  5. 微信小程序之顶部固定和底部固定

    顶部固定 <view style="position:fixed;top:0;"> ...... </view> 底部固定 <view style=& ...

  6. Hadoop创建/删除文件夹出错

    log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFact ...

  7. Ubuntu中开启和关闭防火墙-摘自网络

    1.关闭ubuntu的防火墙 ufw disable开启防火墙ufw enable 2.卸载了iptablesapt-get remove iptables3.关闭ubuntu中的防火墙的其余命令ip ...

  8. css冻结列的效果

    <!DOCTYPE html><html lang="zh-cn"><head><meta charset="utf-8&quo ...

  9. 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带 ...

  10. [sql]mysql5.6cmake安装/mysql5.7二进制安装

    centos7上cmake编译安装mysql-5.6.36.tar.gz 系统环境 - 环境(安装前规划好主机名,mysql编译过程会用) [root@n1 mysql-5.6.36]# cat /e ...