解析xx.properties文件信息

来源:互联网 发布:pubmed数据库是什么 编辑:程序博客网 时间:2024/05/21 15:02

在某些配置信息需要初始化的时候,为了便于维护和修改,往往选择将他们的配置信息放在一个xx.properties文件中。

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

eg:

db.properties文件:

#oracledrivername=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@172.17.192.116:1521:m2muser=rootpwd=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));        }    }}




0 0
原创粉丝点击