2013-07-04 44 views
1

我想uptil兩個目錄的路徑添加到當前類file.I上述路徑的兩個目錄上面我用這樣的:獲取路徑的路徑,當前類文件

Test.class.getProtectionDomain().getCodeSource().getLocation().getPath() 

,但只給出了路徑當前類文件,而我想獲得路徑uptil父母的這個。 有沒有任何干淨的方式來獲取這個沒有使用子字符串?

+1

您可以添加''../將告訴操作系統從當前目錄上升一級:'pathToCurrentDirectory +「../../」' – Leri

回答

2

如果從URL創建一個File你可以調用它getParentFile()

URL fileUrl = Test.class.getProtectionDomain().getCodeSource().getLocation(); 
File file = new File(fileUrl.toURI); 
String grandParent = file.getParentFile().getParent();

這也應該工作:

String grandParent = Test.class.getResource("../../").toString();