解析property文件

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;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值