package com.jg.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
/**
* This is the single entry point for accessing configuration properties.
*
*/
public class SystemConfig
{
private static Properties mConfig;
static
{
mConfig = new Properties();
try{
//绝对路径
mConfig.load(new BufferedInputStream(new FileInputStream(System.getProperty("user.dir") + "/system-redis.properties")));
}catch(Exception exp1){
try{
//相对路径
mConfig.load(SystemConfig.class.getClassLoader().getResourceAsStream("system-redis.properties"));
}catch(Exception exp2){
exp2.printStackTrace();
Thread.currentThread().stop();
}
}
}
// no, you may not instantiate this class :p
private SystemConfig()
{
}
/**
* Retrieve a property value
*
* @param key
* Name of the property
* @return String Value of property requested, null if not found
*/
public static String getProperty(String key)
{
return mConfig.getProperty(key);
}
public static String getProperty(String key, String defaultValue)
{
String value = SystemConfig.getProperty(key);
if (value == null)
{
return defaultValue;
}
return value;
}
/**
* Retrieve a property as a boolean ... defaults to false if not present.
*/
public static boolean getBooleanProperty(String name)
{
return getBooleanProperty(name, false);
}
/**
* Retrieve a property as a boolean ... with specified default if not present.
*/
public static boolean getBooleanProperty(String name, boolean defaultValue)
{
// get the value first, then convert
String value = SystemConfig.getProperty(name);
if (value == null)
{
return defaultValue;
}
return (new Boolean(value)).booleanValue();
}
/**
* Retrieve a property as a int ... defaults to 0 if not present.
*
* @param name
* @return
*/
public static int getIntProperty(String name)
{
return getIntProperty(name, 0);
}
/**
* 返回指定默认值
*
* @param name
* @param defaultValue
* @return
*/
public static int getIntProperty(String name, int defaultValue)
{
// get the value first, then convert
String value = SystemConfig.getProperty(name);
if (value == null)
{
return defaultValue;
}
try
{
return Integer.parseInt(value);
}
catch (NumberFormatException e)
{
return defaultValue;
}
}
/**
* 返回指定默认值的int数组
*
* @param name
* @param defaultValue
* @author wondtech liangming
* @date 2008-07-25
* @return int[]
*/
public static int[] getIntPropertyArray(String name, int[] defaultValue)
{
// get the value first, then convert
String value = SystemConfig.getProperty(name);
if (value == null)
{
return defaultValue;
}
try
{
String[] propertyArray = value.split(",");// 将字符用逗开分离
int[] result = new int[propertyArray.length];
for (int i = 0; i < propertyArray.length; i++)
{//
result[i] = Integer.parseInt(propertyArray[i]);
}
return result;
}
catch (NumberFormatException e)
{
return defaultValue;
}
}
/**
* 返回指定默认值的boolean数组
*
* @param name
* @param defaultValue
* @author wondtech liangming
* @date 2008-07-25
* @return boolean[]
*/
public static boolean[] getBooleanPropertyArray(String name, boolean[] defaultValue)
{
// get the value first, then convert
String value = SystemConfig.getProperty(name);
if (value == null)
{
return defaultValue;
}
try
{
String[] propertyArray = value.split(",");// 将字符用逗开分离
boolean[] result = new boolean[propertyArray.length];
for (int i = 0; i < propertyArray.length; i++)
{//
result[i] = (new Boolean(propertyArray[i])).booleanValue();
}
return result;
}
catch (NumberFormatException e)
{
return defaultValue;
}
}
/**
* 返回指定默认值的str数组
*
* @param name
* @param defaultValue
* @author wondtech liangming
* @date 2008-07-25
* @return String[]
*/
public static String[] getPropertyArray(String name, String[] defaultValue)
{
// get the value first, then convert
String value = SystemConfig.getProperty(name);
if (value == null)
{
return defaultValue;
}
try
{
String[] propertyArray = value.split(",");// 将字符用逗开分离
return propertyArray;
}
catch (NumberFormatException e)
{
return defaultValue;
}
}
/**
* 返回指定默认值的str数组
*
* @param name
* @param defaultValue
* @author wondtech liangming
* @date 2008-07-25
* @return String[]
*/
public static String[] getPropertyArray(String name)
{
// get the value first, then convert
String value = SystemConfig.getProperty(name);
if (value == null)
{
return null;
}
try
{
String[] propertyArray = value.split(",");// 将字符用逗开分离
return propertyArray;
}
catch (NumberFormatException e)
{
return null;
}
}
/**
* Retrieve all property keys
*
* @return Enumeration A list of all keys
*/
public static Enumeration keys()
{
return mConfig.keys();
}
public static Map<String,Object> getPropertyMap(String name)
{
String[] maps = getPropertyArray(name);
Map<String,Object> map = new TreeMap<String,Object>();
try
{
for (String str : maps)
{
String[] array = str.split(":");
if (array.length > 1)
{
map.put(array[0], array[1]);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return map;
}
}
解析property文件
最新推荐文章于 2021-07-19 15:56:37 发布