jarパッケージの外部にあるlog4j構成ファイルを読み取ります
背景:すべての構成ファイルを出力し、jarパッケージ内のすべての構成ファイルを削除しました。jarパッケージの同じディレクトリにある構成ファイルを読み取る必要があります。
- xmlファイルは構成されていないようで、直接読み取ることができます。
- log4j構成ファイルは、main関数に次のステートメントを追加する必要があります(main関数の下の最初の行に追加しました)
PropertyConfigurator.configure(System.getProperty("user.dir") + "/log4j.properties");
注:プロパティファイルは、デフォルトでsrcディレクトリから読み取られます
。2次または3次ディレクトリに配置されていると思われる場合は、「/ 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;
}