解析xx.properties文件信息

在某些配置信息需要初始化的時候,爲了便於維護和修改,往往選擇將他們的配置信息放在一個xx.properties文件中。

---------------------------------------------------

eg:

db.properties文件:

#oracle
drivername=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@172.17.192.116:1521:m2m
user=root
pwd=root

---------------------------------------------------

代碼演示:

public class PropertiesUtil
{
    public static Properties getProperties(String fileName)
    {
        Properties props = new Properties();
        InputStream ips = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
        try
        {
            props.load(ips);
        }
        catch (IOException e)
        {
            LogUtils.runDebug(e.getMessage());
        }
        finally
        {
            try
            {
                ips.close();
            }
            catch (IOException e)
            {
                LogUtils.runDebug(e.getMessage());
            }
        }
        return props;
    }
    
    @SuppressWarnings("unchecked")
    public static void readProperties(String fileName)
    {
        Properties props = getProperties(fileName);
        Enumeration<String> en = null;
        
        en = (Enumeration<String>)props.propertyNames();
        String key = null;
        while (en.hasMoreElements())
        {
            key = en.nextElement();
            LogUtils.runInfo(key + "---" + props.getProperty(key));
        }
    }
}




發佈了8 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章