Read the log4j configuration file outside the jar package
Background: Put all the configuration files out, and I deleted all the configuration files in the jar package. Need to read the configuration file in the same directory of the jar package.
- The xml file does not seem to be configured and can be read directly.
- The log4j configuration file needs to add the following statement in the main function (I added the first line under the main function)
PropertyConfigurator.configure(System.getProperty("user.dir") + "/log4j.properties");
Note: My properties file is read from the src directory by default. If it
seems to be placed in the secondary or tertiary directory, "/log4j.properties" The configuration here needs to be changed. I didn't use it so I didn't try it.
3. How to read the ini file
Related code:
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;
}