1。当多人合作开发程序时,都使用了log4j,而且各自写了各自的log4j,properties配置文件 ,
在程序合并的时候,如果要合并配置文件的话,在后期开发维护又会产生一些不便。 //copyright © li zong bo
而log4j自身的配置信息,在读取新配置文件的时候,老配置文件的信息不会叠加。
因此自己写了个下面的方法。用来兼容老的配置信息的读取,也增强了配置文件的灵活性。
使用方法,只要配置文件名字满足log4j*.properties,再指定配置文件存放的文件夹就可以了。
/* import org.apache.log4j.PropertyConfigurator;
PropertyConfigurator.configure(Properties properties);
使用这个方法来调用即可。 //copyright © li zongbo
*/
public static Properties loadproperties(String dir) throws FileNotFoundException, IOException { return loadproperties(dir, null); }
//copyright © lizongbo
public static Properties loadproperties(String dir, String fileNamestartWith) throws FileNotFoundException, IOException { if (fileNamestartWith == null) { fileNamestartWith = "log4j"; }
//copyright © li zongbo
Properties pro = new Properties(); File fdir = new File(dir);
//copyright © lizong bo
if (fdir.isFile()) { if (fdir.getName().toLowerCase().endsWith(".properties")) { pro.load(new FileInputStream(fdir)); } } else { File[] fspro = fdir.listFiles();
//copyright © lizongbo
for (int i = 0; i < fspro.length; i++) { if (fspro[i].getName().startsWith(fileNamestartWith) && fspro[i].getName().toLowerCase().endsWith(".properties")) { Properties temppro = new Properties(); temppro.load(new FileInputStream(fspro[i])); //copyright © lizongbo
//copyright © lizongbo
for (Enumeration e = temppro.keys(); e.hasMoreElements();) { String key = (String) e.nextElement(); pro.setProperty(key, temppro.getProperty(key)); } } } } //copyright © lizongbo
//copyright © lizongbo
return pro; } 
|