配置文件读取
来源:互联网 发布:淘宝店铺为什么没访客 编辑:程序博客网 时间:2024/05/21 18:49
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
/**
* 用于加载、读取propertie配置文件的工具类
*/
public class PropertyUtil {
static Logger logger = Logger.getLogger(PropertyUtil.class);
private static List<String> propertyList = new ArrayList<String>();
private static Properties properties = new Properties();
/**
* 加载配置文件
* @param propertyFileName
*/
private static void loadProperties(String propertyFileName){
if(!alreadyLoaded(propertyFileName)){
InputStream in = null;
try {
in = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFileName);
if(in != null){
properties.load(in);
propertyList.add(propertyFileName);
}
} catch (IOException e) {
logger.error("error:"+e.getMessage(),e);
}
finally {
IOUtils.closeQuietly(in);
}
}
}
/**
* 是否加载过此配置文件
* @return
*/
private static boolean alreadyLoaded(String propertyFileName){
return propertyList.contains(propertyFileName);
}
/**
* 根据配置文件名称和健名获取值
* @param propertyFileName 配置文件名称
* @param key 健名
* @param default_value 默认值,当根据key取到的value为空时返回default_value
* @param <T>
* @return
*/
public static int getIntValue(String propertyFileName,String key,int default_value){
loadProperties(propertyFileName);
return properties.get(key)==null?default_value:new Integer(properties.get(key)+"");
}
public static int getIntValue(String propertyFileName,String key){
loadProperties(propertyFileName);
if(properties.get(key)==null){
throw new IllegalArgumentException("Property:"+propertyFileName+" was not found!");
}
return new Integer(properties.get(key)+"");
}
public static String getValue(String propertyFileName,String key,String default_value){
loadProperties(propertyFileName);
return properties.get(key)==null?default_value:(String)properties.get(key);
}
public static String getValue(String propertyFileName,String key){
loadProperties(propertyFileName);
if(properties.get(key)==null){
throw new IllegalArgumentException("Property:"+propertyFileName+" was not found!");
}
return (String)properties.get(key);
}
public static boolean 个体tBooleanValue(String propertyFileName,String key,boolean default_value){
loadProperties(propertyFileName);
return properties.get(key)==null?default_value:new Boolean(properties.get(key)+"");
}
public static boolean getBooleanValue(String propertyFileName,String key){
loadProperties(propertyFileName);
if(properties.get(key)==null){
throw new IllegalArgumentException("Property:"+propertyFileName+" was not found!");
}
return new Boolean(properties.get(key)+"");
}
public static void main(String[] args) {
System.out.println(getValue("props/common.properties","saf.registry.address"));
}
}
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
/**
* 用于加载、读取propertie配置文件的工具类
*/
public class PropertyUtil {
static Logger logger = Logger.getLogger(PropertyUtil.class);
private static List<String> propertyList = new ArrayList<String>();
private static Properties properties = new Properties();
/**
* 加载配置文件
* @param propertyFileName
*/
private static void loadProperties(String propertyFileName){
if(!alreadyLoaded(propertyFileName)){
InputStream in = null;
try {
in = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFileName);
if(in != null){
properties.load(in);
propertyList.add(propertyFileName);
}
} catch (IOException e) {
logger.error("error:"+e.getMessage(),e);
}
finally {
IOUtils.closeQuietly(in);
}
}
}
/**
* 是否加载过此配置文件
* @return
*/
private static boolean alreadyLoaded(String propertyFileName){
return propertyList.contains(propertyFileName);
}
/**
* 根据配置文件名称和健名获取值
* @param propertyFileName 配置文件名称
* @param key 健名
* @param default_value 默认值,当根据key取到的value为空时返回default_value
* @param <T>
* @return
*/
public static int getIntValue(String propertyFileName,String key,int default_value){
loadProperties(propertyFileName);
return properties.get(key)==null?default_value:new Integer(properties.get(key)+"");
}
public static int getIntValue(String propertyFileName,String key){
loadProperties(propertyFileName);
if(properties.get(key)==null){
throw new IllegalArgumentException("Property:"+propertyFileName+" was not found!");
}
return new Integer(properties.get(key)+"");
}
public static String getValue(String propertyFileName,String key,String default_value){
loadProperties(propertyFileName);
return properties.get(key)==null?default_value:(String)properties.get(key);
}
public static String getValue(String propertyFileName,String key){
loadProperties(propertyFileName);
if(properties.get(key)==null){
throw new IllegalArgumentException("Property:"+propertyFileName+" was not found!");
}
return (String)properties.get(key);
}
public static boolean 个体tBooleanValue(String propertyFileName,String key,boolean default_value){
loadProperties(propertyFileName);
return properties.get(key)==null?default_value:new Boolean(properties.get(key)+"");
}
public static boolean getBooleanValue(String propertyFileName,String key){
loadProperties(propertyFileName);
if(properties.get(key)==null){
throw new IllegalArgumentException("Property:"+propertyFileName+" was not found!");
}
return new Boolean(properties.get(key)+"");
}
public static void main(String[] args) {
System.out.println(getValue("props/common.properties","saf.registry.address"));
}
}
0 0
- 读取配置文件
- 读取 配置文件
- 读取配置文件
- 读取配置文件
- 配置文件读取
- 配置文件读取
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 配置文件读取
- 读取配置文件
- Yii 框架创建自己的 web 应用
- 分页的封装工具类
- 约瑟夫环问题
- 杭电1019
- MATLAB中存储中间数据以避免重复计算
- 配置文件读取
- 本博客LeetCode题解索引及各类算法问题小结汇总(C++实现)
- Spring Boot 集成Swagger
- python爬虫介绍
- 欢迎使用CSDN-markdown编辑器
- malloc、calloc、realloc的区别
- 八个小技巧教你做出舒服的MG动画
- POJ 3041 Asteroids 匈牙利算法模板题
- iOS 导航栏的隐藏以及右滑返回的问题