/**
* Copyright(c) 2005 zjhcsoft Techonologies, Ltd.
*
* History:
* 2010-3-4 14:10:33 Created by Tiwen
*/
package com.zjhcsoft.modules.configuration;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.configuration.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zjhcsoft.common.util.StringUtil;
/**
* 系统配置文件参获取类
*
* @author <a href="mailto:Tiwen@qq.com">Tiwen</a>
* @version 1.0 2010-3-4 14:10:33
*/
public class SystemConfiguration
{
private static Logger log = LoggerFactory.getLogger(SystemConfiguration.class);
private static SystemConfiguration systemConfiguration = new SystemConfiguration();
private static List<String> locations;
private SystemConfiguration()
{
}
public static SystemConfiguration getInstance()
{
if (!isRead) {
reload();
isRead = true;
}
return systemConfiguration;
}
/**
* 重新加载配置文件
*/
public static void reload()
{
if (locations == null) {
log.warn("没有配置文件,无法读取");
return;
}
//预先清空原先的配置集合
compositeConfiguration.clear();
Configuration conf = null;
int index = 0;
int count = 0;
for (String location : locations) {
if (StringUtil.isValid(location)) {
try {
log.debug("开始读取系统配置文件[{}]", location);
if (location.toLowerCase().endsWith(".xml")) {
conf = new XMLConfiguration(location);
}
else {
conf = new PropertiesConfiguration(location);
}
compositeConfiguration.addConfiguration(conf);
index = 0;
for (Iterator<String> it = conf.getKeys(); it.hasNext(); ) {
index++;
count++;
String key = it.next();
log.debug("配置文件信息:[key = {} , value = {} ]", key, conf.getString(key));
}
log.info("【{}】配置文件完毕,SystemConfig总共加载了 {} 个配置信息", location, index);
}
catch (ConfigurationException e) {
log.error("SystemConfiguration读取系统配置出现错误:", e);
}
}
}
log.info("所有配置文件完毕,systemConfiguration总共加载了 {} 个配置信息", count);
}
private static CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
private static boolean isRead = false;
/**
* 读取key,返回boolean结果,如果没有找到对应键值,则返回false
* @param key 键
* @return boolean结果
*/
public static boolean getBoolean(String key)
{
return getBoolean(key, false);
}
/**
* 读取key,返回boolean结果,如果没有找到对应键值,则返回默认值
* @param key 键
* @param defaultValue 默认值
* @return boolean结果
*/
public static boolean getBoolean(String key, boolean defaultValue)
{
return compositeConfiguration.getBoolean(key, defaultValue);
}
/**
* 读取key,返回字符串结果,如果没有找到对应键值,则返回null
* @param key 键
* @return 键值字符串
*/
public static String getString(String key)
{
return getString(key, null);
}
/**
* 读取key,返回字符串结果,如果没有找到对应键值,则返回默认值
* @param key 键
* @param defaultValue 默认值
* @return 键值字符串
*/
public static String getString(String key, String defaultValue)
{
return compositeConfiguration.getString(key, defaultValue);
}
/**
* 读取key,返回int结果,如果没有找到对应键值,则返回0
* @param key 键
* @return 键值
*/
public static int getInt(String key)
{
return getInt(key, 0);
}
/**
* 读取key,返回int结果,如果没有找到对应键值,则返回默认值
* @param key 键
* @param defaultValue 默认值
* @return 键值
*/
public static int getInt(String key, int defaultValue)
{
return compositeConfiguration.getInt(key, defaultValue);
}
/**
* 读取key,返回double结果,如果没有找到对应键值,则返回0f
* @param key 键
* @return 键值
*/
public static double getDouble(String key)
{
return getDouble(key, 0d);
}
/**
* 读取key,返回double结果,如果没有找到对应键值,则返回默认值
* @param key 键
* @param defaultValue 默认值
* @return 键值
*/
public static double getDouble(String key, double defaultValue)
{
return compositeConfiguration.getDouble(key, defaultValue);
}
/**
* 读取key,返回key结果,如果没有找到对应键值,则返回0
* @param key 键
* @return 键值
*/
public static byte getByte(String key)
{
return getByte(key, (byte) 0);
}
/**
* 读取key,返回key结果,如果没有找到对应键值,则返回默认值
* @param key 键
* @param defaultValue 默认值
* @return 键值
*/
public static byte getByte(String key, byte defaultValue)
{
return compositeConfiguration.getByte(key, defaultValue);
}
/**
* 读取key,返回key结果,如果没有找到对应键值,则返回0f
* @param key 键
* @return 键值
*/
public static float getFloat(String key)
{
return getFloat(key, 0f);
}
/**
* 读取key,返回key结果,如果没有找到对应键值,则返回默认值
* @param key 键
* @param defaultValue 默认值
* @return 键值
*/
public static float getFloat(String key, float defaultValue)
{
return compositeConfiguration.getFloat(key, defaultValue);
}
/**
* 读取key,返回key结果,如果没有找到对应键值,则返回0l
* @param key 键
* @return 键值
*/
public static long getLong(String key)
{
return getLong(key, 0l);
}
/**
* 读取key,返回key结果,如果没有找到对应键值,则返回默认值
* @param key 键
* @param defaultValue 默认值
* @return 键值
*/
public static long getLong(String key, long defaultValue)
{
return compositeConfiguration.getLong(key, defaultValue);
}
/**
* 读取key,返回key结果,如果没有找到对应键值,则返回0
* @param key 键
* @return 键值
*/
public static short getShort(String key)
{
return getShort(key, (short) 0);
}
/**
* 读取key,返回key结果,如果没有找到对应键值,则返回默认值
* @param key 键
* @param defaultValue 默认值
* @return 键值
*/
public static short getShort(String key, short defaultValue)
{
return compositeConfiguration.getShort(key, defaultValue);
}
public List<String> getLocations()
{
return locations;
}
public void setLocations(List<String> locations)
{
this.locations = locations;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
配置文件读取JAVA工具类
共24个文件
jar:10个
xml:4个
java:2个
5星 · 超过95%的资源 需积分: 50 69 下载量 176 浏览量
2011-12-02
22:26:35
上传
评论 1
收藏 3.89MB RAR 举报
温馨提示
系统开发的时候,都会涉及到一些配置文件的读取,为方便从配置文件中读取我们预先设置的值,我做了一个简单配置文件读取工具类。这个类主要基于apache开源项目commons-configuration,附件是该工具类完整代码,仅供大家学习使用。
资源推荐
资源详情
资源评论
收起资源包目录
SystemConfig.rar (24个子文件)
SystemConfig
src
com
zjhcsoft
modules
configuration
SystemConfiguration.java 8KB
bin
com
zjhcsoft
modules
configuration
SystemConfigurationTest.class 1KB
SystemConfiguration.class 5KB
test.properties 26B
test.xml 102B
modules.xml 746B
.classpath 972B
.settings
org.eclipse.core.resources.prefs 108B
org.eclipse.jdt.core.prefs 629B
.project 388B
lib
zjhcsoft-base.jar 58KB
commons-configuration-1.6.jar 292KB
spring-test-2.5.5.jar 176KB
commons-lang.jar 273KB
commons-logging.jar 52KB
spring.jar 2.71MB
slf4j-log4j12-1.5.6.jar 9KB
commons-collections.jar 558KB
slf4j-api-1.5.6.jar 22KB
log4j-1.2.14.jar 359KB
test
src
com
zjhcsoft
modules
configuration
SystemConfigurationTest.java 1004B
test.properties 26B
test.xml 102B
modules.xml 746B
共 24 条
- 1
资源评论
- zhangwonderful2014-05-25不错的配置文件读取工具类,学习了,谢谢分享。
- lujiahuawin2013-05-21不错可以玩玩
- hzai2014-07-04谢谢分享,以后可以用上了。
- 浅笑黄花伊人泪2015-02-01jar 包 读xml有问题
Tiwen
- 粉丝: 51
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功