一、前言
许多时候,系统都需要一个配置文件,重复编写这些配置文件读取程序很烦恼,又浪费时间 ,下面给一个通用的方法
二、方法论
好吧!我们用一个简单的单例模式来解决这个问题,一般来说配置文件都是加载一次就够了,最简单的单例模式(实践比空谈好)
1、私有静态对象
private static Config cfg = null;
2、构造器私有化
private Config() {
properties = new Properties();
InputStream is = null;
try {
is = Config.class.getResourceAsStream(path);
properties.load(is);
} catch (Exception exception) {
System.out.println("Can't read the properties file. ");
} finally {
try {
if (is != null) {
is.close();
}
} catch (IO