commons configuration可以很方便的访问配置文件和xml文件中的的内容。Commons Configuration 是为了提供对属性文件、XML文件、JNDI资源、来自JDBC Datasource数据的访问。

  官方文档:http://commons.apache.org/proper/commons-configuration/

我们研究configuration1版本的,最新的是2的版本,暂时没使用。

1、Maven中引入相关的jar

<!-- 配置文件读取 -->
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>

2、读取properties文件的内容

(1)新建一个data.properties

name=parry
port=21
flag=true
users=Tom,parry

(2)工具类读取

package icp;

import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; public class Test { public static final String fileName = "data.properties"; public static PropertiesConfiguration cfg = null; static {
try {
cfg = new PropertiesConfiguration(fileName);
} catch (ConfigurationException e) {
e.printStackTrace();
}
// 当文件的内容发生改变时,配置对象也会刷新
cfg.setReloadingStrategy(new FileChangedReloadingStrategy());
}
  // 读String
public static String getStringValue(String key) {
return cfg.getString(key);
}
  // 读int
public static int getIntValue(String key) {
return cfg.getInt(key);
}
  // 读boolean
public static boolean getBooleanValue(String key) {
return cfg.getBoolean(key);
}
  // 读List
public static List<?> getListValue(String key) {
return cfg.getList(key);
}
  // 读数组
public static String[] getArrayValue(String key) {
return cfg.getStringArray(key);
} }

(3)测试

public static void main(String[] args) {
String name = Test.getStringValue("name");
System.out.println("name:" + name);
int port = Test.getIntValue("port");
System.out.println("port:" + port);
boolean flag = Test.getBooleanValue("flag");
System.out.println("flag:" + flag);
List<String> users = (List<String>) Test.getListValue("users");
for (String user : users) {
System.out.println("user:" + user);
}
}

3、读取XML配置文件

(1)新建一个XMl文件

<?xml version="1.0" encoding="UTF-8"?>
<config>
<database>
<url>127.0.0.1</url>
<port>1521</port>
<login>admin</login>
<password>pass</password>
</database>
</config>

(2)读取XML配置的工具文件

package icp;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; public class XmlTest { public static final String fileName = "XMLProperties.xml"; public static XMLConfiguration cfg = null; static {
try {
cfg = new XMLConfiguration(fileName);
} catch (ConfigurationException e) {
e.printStackTrace();
}
// 配置文件 发生变化就重新加载
cfg.setReloadingStrategy(new FileChangedReloadingStrategy());
} public static String getStringValue(String key) {
return cfg.getString(key);
} public static int getIntValue(String key) {
return cfg.getInt(key);
}
}

这里只是添加读取String 和读取Int的方法,其他的方法类似。

(3)测试

public static void main(String[] args) {
String url = XmlTest.getStringValue("database.url");
System.out.println("url:" + url);
int port =XmlTest.getIntValue("database.port");
System.out.println("port:"+port);
}

由于项目中,经常有多个配置文件,最后提供一个工具类:

public class PropertiesUtil {

    public static Map<String, Object> configMap = new ConcurrentHashMap<String, Object>();

    public static String getStringValue(String fileName, String key) {
if (!configMap.containsKey(key)) {
PropertiesUtil.initConfig(fileName);
}
if (fileName.endsWith(".properties")) {
PropertiesConfiguration cfg = (PropertiesConfiguration) configMap.get(fileName);
return cfg.getString(key);
} else if (fileName.endsWith(".xml")) {
XMLConfiguration cfg = (XMLConfiguration) configMap.get(fileName);
return cfg.getString(key);
}
return null;
} private static void initConfig(String fileName) {
try {
if (fileName.endsWith(".xml")) {
XMLConfiguration cfg = new XMLConfiguration(fileName);
configMap.put(fileName, cfg);
} else if (fileName.endsWith(".properties")) {
PropertiesConfiguration cfg = new PropertiesConfiguration(fileName);
configMap.put(fileName, cfg);
}
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}

测试:

public static void main(String[] args) {
String name= PropertiesUtil.getStringValue("data.properties", "name");
System.out.println(name);
String url = PropertiesUtil.getStringValue("data.xml", "database.url");
System.out.println(url);
}

非原创,来源地址:https://www.cnblogs.com/parryyang/p/6197685.html

Configutation读取properties文件信息的更多相关文章

  1. java读取properties 文件信息

      src下config/tank.properties文件 initTankCount=10 ReinitTankCount=8 Etmspeed=15 Mtmspeed=15 MTankCount ...

  2. spring boot --- 使用 注解 读取 properties 文件 信息

    1.前言 以前使用spring MVC框架 ,读取properties 配置文件需要写一大堆东西 ,也许 那时候 不怎么使用注解 ,现在使用spring boot ,发现了非常简便的办法---使用注解 ...

  3. 用eclipse做项目中常遇到的问题-如何创建并读取properties文件

    在用eclipse做项目开发的时候我们常常会将一些重要的内容写在配置文件里面, 特别是连接数据库的url,username,password等信息,我们常常会新建一个properties文件将所有信息 ...

  4. java读取properties配置文件信息

    一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...

  5. Java的Properties类和读取.properties文件

    一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必 ...

  6. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  7. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

  8. Java读取properties文件连接数据库

    先说为什么要有这种东西,或者我们为什么要用这种方式来写,先看经常用的方法,我们经常写的 package util; import java.sql.Connection; import java.sq ...

  9. Java读取properties文件工具类并解决控制台中文乱码

    1.建立properts文件(error.message.properties) HTTP201= 请求成功并且服务器创建了新的资源 2.在spring-mvc.xml文件(applicationCo ...

随机推荐

  1. Java List集合冒泡法排序的两种实现

    冒泡排序(Bubble Sort)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已 ...

  2. 关于spring session redis共享session的跨子域的处理

    安装完redis, spring端只要下面这两个bean配置上就可以用了 <?xml version="1.0" encoding="UTF-8"?> ...

  3. springboot redis多数据源设置

    遇到这样一个需求:运营人员在发布内容的时候可以选择性的发布到测试库.开发库和线上库. 项目使用的是spring boot集成redis,实现如下: 1. 引入依赖 <dependency> ...

  4. 梅尔罗斯百度云在线观看迅雷下载Patrick Melrose磁力BT下载

    原名:Patrick Melrose 地区:英国 语言:英语 首播:2018-05-12(美国) 电视台:Showtime 类型:剧情 别名:浮生若梦 编剧:大卫·尼克尔森 导演:爱德华·贝尔格 主演 ...

  5. 盾牌第一至七季/全集The Shield迅雷下载

    英文译名The Shield,第1-7季(2002-2008)FX.本季看点:<盾牌>一部极具争议性的连续剧,打破了传统警匪片套路,刻画了性格复杂的警察,他们在与各种罪案做斗争的同时,也面 ...

  6. Android之TextView部分颜色变动

    public class StringHandleExampleActivity extends Activity { /** Called when the activity is first cr ...

  7. window.opener()方法

    <!DOCTYPE html><html><head><meta charset="GBK"><title>菜鸟教程(r ...

  8. SQL Server中取汉字拼音的函数

    ))     ) )     )          )         ),   py          end     return @pinyin END GOSELECT dbo.fn_GetP ...

  9. win32多线程-新版本MtVerify.h

    api调用错误诊断宏,对GetLastError()函数的封装,并解析错误 从网上找的版本并进行了部分修改 /* * MtVerify.h * * The function PrintError() ...

  10. Remote Desktop Session中如何触发Ctrl+Alt+Delete?

    Ctrl+Alt+End is a keyboard shortcut used in a Remote Desktop Session to display the security dialog ...