Configutation读取properties文件信息
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文件信息的更多相关文章
- java读取properties 文件信息
src下config/tank.properties文件 initTankCount=10 ReinitTankCount=8 Etmspeed=15 Mtmspeed=15 MTankCount ...
- spring boot --- 使用 注解 读取 properties 文件 信息
1.前言 以前使用spring MVC框架 ,读取properties 配置文件需要写一大堆东西 ,也许 那时候 不怎么使用注解 ,现在使用spring boot ,发现了非常简便的办法---使用注解 ...
- 用eclipse做项目中常遇到的问题-如何创建并读取properties文件
在用eclipse做项目开发的时候我们常常会将一些重要的内容写在配置文件里面, 特别是连接数据库的url,username,password等信息,我们常常会新建一个properties文件将所有信息 ...
- java读取properties配置文件信息
一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...
- Java的Properties类和读取.properties文件
一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必 ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java基础学习总结——java读取properties文件总结
摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...
- Java读取properties文件连接数据库
先说为什么要有这种东西,或者我们为什么要用这种方式来写,先看经常用的方法,我们经常写的 package util; import java.sql.Connection; import java.sq ...
- Java读取properties文件工具类并解决控制台中文乱码
1.建立properts文件(error.message.properties) HTTP201= 请求成功并且服务器创建了新的资源 2.在spring-mvc.xml文件(applicationCo ...
随机推荐
- linux 统计文件夹空间
du -sh * | sort -nr
- ImageView中scaleType属性详解
scaleType是指定图片的拉伸方式的一个属性,下面是具体的示例和介绍: <LinearLayout xmlns:android="http://schemas.android.co ...
- [转]在ubuntu linux下以编译方式安装LAMP(apache mysql php)环境
FROM : http://www.cnblogs.com/eleganthqy/archive/2010/02/28/1675217.html 最近转向到了使用ubuntu做桌面,安装好系统以来一直 ...
- vim的翻页、跳转到某一行功能
第一种方式 :$ 跳转到最后一行 :1 跳转到第一行 :n 跳转到第n行 第二种方式 shift+g 跳转到最后一行 gg 跳转到第一行 command+上下箭头
- rsync推送备份服务器(Linux)
rsync推送备份服务器备份服务器操作需被备份的服务器操作批处理shell备份服务器操作#编辑配置文件vi /etc/rsyncd.conf uid = root #运行RSYNC守护进程的用户gid ...
- 第二章 微服务网关基础组件 - zuul入门
一.zuul简介 1.作用 zuul使用一系列的filter实现以下功能 认证和安全 - 对每一个resource进行身份认证 追踪和监控 - 实时观察后端微服务的TPS.响应时间,失败数量等准确的信 ...
- 第一章 Java加解密简介
1.加密算法: 移位.替代(古典加密) 对称加密:DES.AES 非对称加密:RSA 散列函数算法(单向加密):MD5.SHA.Mac 数字签名算法:RSA.DSA 其中,前三种主要完成数据的加解密: ...
- go语言之进阶篇error接口的使用
1.error接口的使用 示例: package main import "fmt" import "errors" func main() { //var e ...
- weblogic——服务器搭建与配置
本次操作的内容:weblogic服务器搭建与配置服务 本次操作是主要围绕如何搭建weblogic服务器及配置服务,总共有两大步骤,可划分为六个小步骤: 选取已有环境,准备weblogic压缩包 安装w ...
- 推荐朋友 - LintCode
拼多多笔试第三题 除了题目具体方法值得注意外,数据的输入格外注意 题目 描述 给n个人的朋友名单,告诉你user,请找出user最可能认识的人.(他和user有最多的共同好友且他不是user的朋友) ...