import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyReaderUtil {
private static final String[] files = new String[]{"conf.properties"};
private static final Properties configProps = new Properties();
static{
load(files);
}
/**
* 将构造方法设为私有,不能在外部进行实例化<br>
* <b>创建日期</b>:2011-1-14
* @author <a href="mailto:hemingwang0902@126.com">何明旺</a>
*/
private PropertyReaderUtil(){
}
/**
* 加载properties文件
* @param fileType 文件类型: 0.普通的properties文件; 1.XML格式的properties文件
* @param filePath properties文件路径
*/
private static void load(String... filePath) {
String configFilePath = null;
InputStream is = null;
for (int i = 0; i < filePath.length; i++) {
configFilePath = filePath[i];
try {
if(configFilePath.toLowerCase().endsWith(".xml")){
is = PropertyReaderUtil.class.getClassLoader().getResourceAsStream(configFilePath) ;
configProps.loadFromXML(is);
}else if(configFilePath.toLowerCase().endsWith(".properties")){
is = PropertyReaderUtil.class.getClassLoader().getResourceAsStream(configFilePath) ;
configProps.load(is);
}else{
throw new RuntimeException("can't support the file: " + configFilePath);
}
System.out.println("file \"" + configFilePath + "\" load success!");
} catch (Exception e) {
System.out.println("file \"" + configFilePath + "\" load failed, detailed exception infomation:\n" + e.getMessage());
throw new RuntimeException("file \"" + configFilePath + "\" load failed", e);
}finally{
if (is != null) {
try {
is.close();
} catch (IOException e) {
System.out.println("finished loading all of the configuration file, close the FileInputStream when an exception occurs. \n" + e.getMessage());
}
}
}
}
}
public static Integer getInt(String key) {
return Integer.valueOf(configProps.getProperty(key));
}
/**
* 获取 int 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 对应的配置属性value,如果配置属性的value不为int类型或不存在对应的配置属性,则返回 defaultValue
*/
public static Integer getInt(String key, Integer defaultValue) {
try{
Integer value = getInt(key);
return value == null ? defaultValue : value;
}catch (NumberFormatException e) {
return defaultValue;
}
}
public static Float getFloat(String key) {
return Float.valueOf(configProps.getProperty(key));
}
/**
* 获取 float 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 对应的配置属性value,如果配置属性的value不为float类型或不存在对应的配置属性,则返回 defaultValue
*/
public static Float getFloat(String key, Float defaultValue) {
try {
Float value = getFloat(key);
return value == null ? defaultValue : value;
} catch (NumberFormatException e) {
return defaultValue;
}
}
public static Double getDouble(String key) {
return Double.valueOf(configProps.getProperty(key));
}
/**
* 获取 double 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 对应的配置属性value,如果配置属性的value不为double类型或不存在对应的配置属性,则返回 defaultValue
*/
public static Double getDouble(String key, double defaultValue) {
try {
Double value = getDouble(key);
return value == null ? defaultValue : value;
} catch (NumberFormatException e) {
return defaultValue;
}
}
public static Long getLong(String key) {
return Long.valueOf(configProps.getProperty(key));
}
/**
* 获取 long 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 对应的配置属性value,如果配置属性的value不为long类型或不存在对应的配置属性,则返回 defaultValue
*/
public static Long getLong(String key, long defaultValue) {
try {
Long value = getLong(key);
return value == null ? defaultValue : value;
} catch (NumberFormatException e) {
return defaultValue;
}
}
public static String getString(String key) {
return configProps.getProperty(key);
}
/**
* 获取 String 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 对应的配置属性value
*/
public static String getString(String key, String defaultValue) {
return configProps.getProperty(key, defaultValue);
}
public static Boolean getBoolean(String key) {
return Boolean.valueOf(configProps.getProperty(key));
}
/**
* 获取 boolean 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 如果配置属性的value为""或不存在对应的配置属性,则返回 defaultValue; <br>
* 如果 value 为 on、yes 或 true(均不区分大小写) 则返回 true, 否则返回 false
*/
public static Boolean getBoolean(String key, boolean defaultValue) {
Boolean value = getBoolean(key);
return value == null ? defaultValue : value;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
import java.io.InputStream;
import java.util.Properties;
public class PropertyReaderUtil {
private static final String[] files = new String[]{"conf.properties"};
private static final Properties configProps = new Properties();
static{
load(files);
}
/**
* 将构造方法设为私有,不能在外部进行实例化<br>
* <b>创建日期</b>:2011-1-14
* @author <a href="mailto:hemingwang0902@126.com">何明旺</a>
*/
private PropertyReaderUtil(){
}
/**
* 加载properties文件
* @param fileType 文件类型: 0.普通的properties文件; 1.XML格式的properties文件
* @param filePath properties文件路径
*/
private static void load(String... filePath) {
String configFilePath = null;
InputStream is = null;
for (int i = 0; i < filePath.length; i++) {
configFilePath = filePath[i];
try {
if(configFilePath.toLowerCase().endsWith(".xml")){
is = PropertyReaderUtil.class.getClassLoader().getResourceAsStream(configFilePath) ;
configProps.loadFromXML(is);
}else if(configFilePath.toLowerCase().endsWith(".properties")){
is = PropertyReaderUtil.class.getClassLoader().getResourceAsStream(configFilePath) ;
configProps.load(is);
}else{
throw new RuntimeException("can't support the file: " + configFilePath);
}
System.out.println("file \"" + configFilePath + "\" load success!");
} catch (Exception e) {
System.out.println("file \"" + configFilePath + "\" load failed, detailed exception infomation:\n" + e.getMessage());
throw new RuntimeException("file \"" + configFilePath + "\" load failed", e);
}finally{
if (is != null) {
try {
is.close();
} catch (IOException e) {
System.out.println("finished loading all of the configuration file, close the FileInputStream when an exception occurs. \n" + e.getMessage());
}
}
}
}
}
public static Integer getInt(String key) {
return Integer.valueOf(configProps.getProperty(key));
}
/**
* 获取 int 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 对应的配置属性value,如果配置属性的value不为int类型或不存在对应的配置属性,则返回 defaultValue
*/
public static Integer getInt(String key, Integer defaultValue) {
try{
Integer value = getInt(key);
return value == null ? defaultValue : value;
}catch (NumberFormatException e) {
return defaultValue;
}
}
public static Float getFloat(String key) {
return Float.valueOf(configProps.getProperty(key));
}
/**
* 获取 float 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 对应的配置属性value,如果配置属性的value不为float类型或不存在对应的配置属性,则返回 defaultValue
*/
public static Float getFloat(String key, Float defaultValue) {
try {
Float value = getFloat(key);
return value == null ? defaultValue : value;
} catch (NumberFormatException e) {
return defaultValue;
}
}
public static Double getDouble(String key) {
return Double.valueOf(configProps.getProperty(key));
}
/**
* 获取 double 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 对应的配置属性value,如果配置属性的value不为double类型或不存在对应的配置属性,则返回 defaultValue
*/
public static Double getDouble(String key, double defaultValue) {
try {
Double value = getDouble(key);
return value == null ? defaultValue : value;
} catch (NumberFormatException e) {
return defaultValue;
}
}
public static Long getLong(String key) {
return Long.valueOf(configProps.getProperty(key));
}
/**
* 获取 long 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 对应的配置属性value,如果配置属性的value不为long类型或不存在对应的配置属性,则返回 defaultValue
*/
public static Long getLong(String key, long defaultValue) {
try {
Long value = getLong(key);
return value == null ? defaultValue : value;
} catch (NumberFormatException e) {
return defaultValue;
}
}
public static String getString(String key) {
return configProps.getProperty(key);
}
/**
* 获取 String 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 对应的配置属性value
*/
public static String getString(String key, String defaultValue) {
return configProps.getProperty(key, defaultValue);
}
public static Boolean getBoolean(String key) {
return Boolean.valueOf(configProps.getProperty(key));
}
/**
* 获取 boolean 类型的配置属性的value
*
* @param key
* 配置属性的key
* @param defaultValue
* 默认值
* @return 如果配置属性的value为""或不存在对应的配置属性,则返回 defaultValue; <br>
* 如果 value 为 on、yes 或 true(均不区分大小写) 则返回 true, 否则返回 false
*/
public static Boolean getBoolean(String key, boolean defaultValue) {
Boolean value = getBoolean(key);
return value == null ? defaultValue : value;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}