在某些配置信息需要初始化的时候,为了便于维护和修改,往往选择将他们的配置信息放在一个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));
}
}
}