public class ReadProperties {
/*
* 加载指定路径下的配置文件信息,并获得Properties对象
* (non-Javadoc)
* @see com.voole.interf.IProperties#getProperties()
* @return Properties对象
*/
public static Properties getProperties() {
return getProperties("/config.properties");
}
public static Properties getProperties(String path) {
InputStream inStream = ReadProperties.class.getResourceAsStream(path);
Properties prop = new Properties();
try {
if(inStream != null){
prop.load(inStream);
inStream.close();
}
} catch (IOException e) {
System.out.println("加载指定路径下的配置文件出错!"+e.getMessage());
}
return prop;
}
//使用
public static void main(String[] args) {
Properties pr = ReadProperties.getProperties();
System.out.println("mysql connection -- " + pr.getProperty("SQL_DRIVER"));
}
}
资源文件加载的的类
读取配置文件示例
最新推荐文章于 2025-02-11 18:37:07 发布
本文提供了一个Java示例程序,展示如何从指定路径加载配置文件,并通过Properties类获取配置信息。具体演示了如何加载名为config.properties的配置文件,并读取SQL_DRIVER属性。
3249

被折叠的 条评论
为什么被折叠?



