jar包外读取log4j的配置文件

jar包外读取log4j的配置文件

背景:将配置文件全部外放,jar包中的配置文件我全给删除了。需要读取jar包同目录下的配置文件。
在这里插入图片描述

  1. xml文件貌似不用配置,可以直接读取。
  2. log4j的配置文件需要在main函数中添加如下语句(我添加到了main函数下的第一行)
PropertyConfigurator.configure(System.getProperty("user.dir") + "/log4j.properties");

注意:我这个properties文件默认是读取src目录下的在这里插入图片描述
貌似放在二级或者三级目录下的话"/log4j.properties" 这里的配置要变一变,我没有用到所以也就没尝试。

3.ini文件的读取方式

在这里插入图片描述
相关代码:

ublic Hashtable getConfigFile(){
    
    
		Hashtable htable = new Hashtable();
		System.out.println("获取dbconfig.ini配置参数");
		FileReader fr = null;
		BufferedReader br = null;
		//String Config_dir = this.getClass().getResource("dbconfig.ini").getPath();
		try {
    
    
			fr = new FileReader(new File(FinalParam.Config_dir));
			//fr = new FileReader(new File(Config_dir));
			br = new BufferedReader(fr);
			String tmp_str = null;
			while((tmp_str = br.readLine()) != null){
    
    
				String[] cube_cfg = new String[2];
				if(tmp_str.indexOf("=") != -1){
    
    
					cube_cfg = DoSplitStr(tmp_str);
					htable.put(cube_cfg[0], cube_cfg[1]);
				}else{
    
    
					FinalParam.println("配置文件出现空行,跳过!");
				}
			}
			System.out.println("读取到配置文件配置个数:"+htable.size());
		} catch (Exception e) {
    
    
			// TODO Auto-generated catch block
			FinalParam.println("读取配置文件出错!");
			e.printStackTrace();
		}

		return htable;
	}

//在这个函数中实现的调用
    public Connection initConfig2(Connection conn) {
    
    
        System.out.println("初始化数据库链接");
        htable = dofile.getConfigFile();// 读取配置文件,初始化
        conn = dbconn.getConnection(htable);// 获取全局数据库链接
        System.out.println("获取全局数据库链接");
        //System.out.println("conn:"+conn.toString());
        if (htable == null || htable.isEmpty() || conn == null) {
    
    
            System.out.println("htable参数为空 或者 初始化连接为null");
        }
        return conn;
    }

猜你喜欢

转载自blog.csdn.net/zhuyin6553/article/details/108488741