我有一個JAR文件,我的所有代碼都存檔以便運行。我必須訪問每次運行前需要更改/編輯的屬性文件。我想將屬性文件保存在JAR文件所在的目錄中。有沒有告訴Java從該目錄中獲取屬性文件?注意:我不想將屬性文件保留在主目錄中,也不希望在命令行參數(shù)中傳遞屬性文件的路徑。
3 回答

森林海
TA貢獻(xiàn)2011條經(jīng)驗 獲得超2個贊
我通過其他方式做到了。
Properties prop = new Properties();
try {
File jarPath=new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
String propertiesPath=jarPath.getParentFile().getAbsolutePath();
System.out.println(" propertiesPath-"+propertiesPath);
prop.load(new FileInputStream(propertiesPath+"/importer.properties"));
} catch (IOException e1) {
e1.printStackTrace();
}
獲取Jar文件路徑。
獲取該文件的Parent文件夾。
在InputStreamPath中使用該路徑和您的屬性文件名。
添加回答
舉報
0/150
提交
取消