PropertiesConfiguration是一个配置文件的加载工具类,封装了从配置文件里获取值并转化为基本数据类型的方法。

使用org.apache.commons.configuration2中提供的工具来读取属性文件 
1.创建Java工程 
2.引入所需jar包 
 
3.在src下创建属性文件properties.properties,内容如下:

username=eclipse
password=123456

4.创建工具类PropsUtils

public class PropertyUtils {
public static void main(String[] args) throws FileNotFoundException, ConfigurationException, IOException {
String relativelyPath = System.getProperty("user.dir");//获取当前项目的根目录
PropertiesConfiguration config = new PropertiesConfiguration();
config.read(new FileReader(relativelyPath + "/src/properties.properties"));
String username = config.getString("username");
System.out.println(username);
String password = config.getString("password");
System.out.println(password);
}
}

它来自commons-configuration-1.6.jar。

直接上代码:

  1. public class Config {
  2. private static PropertiesConfiguration config = null;
  3. static {
  4. try {
  5. config = new PropertiesConfiguration(
  6. "config.properties");
  7. } catch (ConfigurationException e) {
  8. e.printStackTrace();
  9. }
  10. }
  11. public static String getString(String key) {
  12. return config.getString(key);
  13. }
  14. public static String getString(String key, String def) {
  15. return config.getString(key, def);
  16. }
  17. public static int getInt(String key) {
  18. return config.getInt(key);
  19. }
  20. public static int getInt(String key, int def) {
  21. return config.getInt(key, def);
  22. }
  23. public static long getLong(String key) {
  24. return config.getLong(key);
  25. }
  26. public static long getLong(String key, long def) {
  27. return config.getLong(key, def);
  28. }
  29. public static float getFloat(String key) {
  30. return config.getFloat(key);
  31. }
  32. public static float getFloat(String key, float def) {
  33. return config.getFloat(key, def);
  34. }
  35. public static double getDouble(String key) {
  36. return config.getDouble(key);
  37. }
  38. public static double getDouble(String key, double def) {
  39. return config.getDouble(key, def);
  40. }
  41. public static boolean getBoolean(String key) {
  42. return config.getBoolean(key);
  43. }
  44. public static boolean getBoolean(String key, boolean def) {
  45. return config.getBoolean(key, def);
  46. }
  47. public static String[] getStringArray(String key) {
  48. return config.getStringArray(key);
  49. }
  50. @SuppressWarnings("unchecked")
  51. public static List getList(String key) {
  52. return config.getList(key);
  53. }
  54. @SuppressWarnings("unchecked")
  55. public static List getList(String key, List def) {
  56. return config.getList(key, def);
  57. }
  58. public static void setProperty(String key, Object value) {
  59. config.setProperty(key, value);
  60. }
  61. }

Commons Configuration是一个Java应用程序的配置管理类库。可以从properties或者xml文件中加载软件的配置信息,用来构建支撑软件运 行的基础环境。在一些配置文件较多较的复杂的情况下,使用该配置工具比较可以简化配置文件的解析和管理。也提高了开发效率和软件的可维护性。

官方主页:http://commons.apache.org/configuration/

它目前支持的配置文件格式有:

Properties files 
XML documents 
Windows INI files 
Property list files (plist) 
JNDI 
JDBC Datasource 
System properties 
Applet parameters 
Servlet parameters

我使用的是目前最新版本1.9,以调用 Properties格式的文件为例,使用方法如下:

基本用法:

1.加载jar包,我使用maven自动加载,pom.xml配置如下:

  1. <dependency>
  2. <groupId>commons-configuration</groupId>
  3. <artifactId>commons-configuration</artifactId>
  4. <version>1.9</version>
  5. </dependency>
  6. <!-- commons-configuration 自动加载的是2.1的版本,编译时会报错,所以再加上这个 -->
  7. <dependency>
  8. <groupId>commons-lang</groupId>
  9. <artifactId>commons-lang</artifactId>
  10. <version>2.6</version>
  11. </dependency>

common-lang这个包要用新版的,如果不写这个依赖,commons-configuration会下载一个2.1旧版,导致编译出错

2.java代码:

  1. PropertiesConfiguration config = new PropertiesConfiguration(“/database.properties”);
  2. String userName = config.getString("name");

除了getString()方法外,还有getBoolean,getDouble,getInteger等不同返回类型的方法可以调用。

进阶用法:

一个项目有会有多个配置文件,这时有个统一的配置文件管理类就很有必要了,我写了一个简单的,大家可以参考下,有不妥的用法也请指出来

1.java类

  1. package com.xxx.xxx.util;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.apache.commons.configuration.ConfigurationException;
  5. import org.apache.commons.configuration.PropertiesConfiguration;
  6. /**
  7. * <p>
  8. * 读取配置文件类
  9. * </p>
  10. * <p>
  11. * 根据配置文件名和属性key返回属性内容,configUtil.get(configFile, property);
  12. * </p>
  13. * @author shengzhi.rensz
  14. *
  15. */
  16. public class configUtil {
  17. private static configUtil initor = new configUtil();
  18. private static Map<String, Object> configMap = new HashMap<String, Object>();
  19. private configUtil() {}
  20. /**
  21. * 获取内容
  22. * @param configFile
  23. * @param property
  24. * @return
  25. */
  26. public static String get(String configFile, String property) {
  27. if(!configMap.containsKey(configFile)) {
  28. initor.initConfig(configFile);
  29. }
  30. PropertiesConfiguration config = (PropertiesConfiguration) configMap.get(configFile);
  31. String value = config.getString(property);
  32. //TODO LOG
  33. return value;
  34. }
  35. /**
  36. * 载入配置文件,初始化后加入map
  37. * @param configFile
  38. */
  39. private synchronized void initConfig(String configFile) {
  40. try {
  41. PropertiesConfiguration config = new PropertiesConfiguration(configFile);
  42. configMap.put(configFile, config);
  43. } catch (ConfigurationException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }

2.调用方法

    1. configUtil.get("/common/velocity.properties", "input.encoding");

PropertiesConfiguration的用法的更多相关文章

  1. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  2. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  3. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  4. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  5. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  6. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  7. 【JavaScript】innerHTML、innerText和outerHTML的用法区别

    用法: <div id="test">   <span style="color:red">test1</span> tes ...

  8. chattr用法

    [root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...

  9. 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)

    vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...

随机推荐

  1. 华为机试 求int型数据在内存中存储时1的个数

    题目描述 输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数. 输入描述: 输入一个整数(int类型) 输出描述: 这个数转换成2进制后,输出1的个数 输入 5 输出 2 普通运算方 ...

  2. HyperLedger Fabric 1.4 超级账本项目(5.4)

    超级账本(Hyperledger)项目分框架类和工具类两种项目,框架类有Hyperledger Burrow.Hyperledger Fabric.Hyperledger Indy.Hyperledg ...

  3. FPGA算法学习(1) -- Cordic(圆周系统之旋转模式)

    三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来计算任意角度的三角函数的值.这种表格在人们刚刚产生三角函数的概念的时候就已经有了,它们通常是通过从已知值(比如sin(π/2)= ...

  4. 贪心算法之Huffman

    Huffman编码,权重越大,离根节点越大.所以就是不断的选取两个最小的树,然后组成一颗新树,加入集合,然后去除已选的两棵树.不断的循环,直到最后的树的集合只剩下一棵,则构建完成,最后输出Huffma ...

  5. c/c++指针理解

    指针的概念 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址.要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占 ...

  6. kindeditor 限制上传图片大小及宽高

    进入:/kindeditor/plugins/image/image.js,替换其中的 self.plugin.imageDialog = function (options)方法,代码为: self ...

  7. MySQL高可用之PXC安装部署(续)

      Preface       Yesterday I implemented a three-nodes PXC,but there were some errors when proceeding ...

  8. Ubuntu 常见错误及解决方法——长期不定时更新

    1. 修复 /etc/sudoers 文件损坏导致不能使用 sudo 命令 这是之前错误地编辑了 /etc/sudoers 这个文件导致的,因此撤销编辑即可,但由于已经不能使用 sudo 命令,因此不 ...

  9. 容器基础(十): 使用kubernetes部署应用

    概述 使用之前的脚本(env/server.py 得到 env/server:v0.1 镜像, env/worker.py 得到 env/worker:v0.1)得到的镜像,在部署好kubernete ...

  10. Window.open()方法参数详解总结(转)

    1, 最基本的弹出窗口代码   window.open('page.html'); 2, 经过设置后的弹出窗口   window.open('page.html', 'newwindow', 'hei ...