Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
log4j的辅助代码,支持合并多个log4j配置文件。

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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;
    }



相关文章

相关软件