2013-06-20 103 views
2

我要上傳的文件存儲在我的項目文件夾本身的Java Swing工程項目的路徑,因此如何讓項目路徑的Java Swing項目如何獲得

+1

您的意思是應用程序當前正在執行的位置?你可以使用'new File(「。」)'並使用'getPath()'來確定你當前的執行位置...... – MadProgrammer

回答

3

我用這個了... ...

System.getProperty("user.dir") 
1

我用下面的代碼位來識別類文件的位置:

private static File findProgramLocation() throws URISyntaxException { 
    File file = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); 
    if (!file.exists()) { 
     throw new RuntimeException("Could not identify program location, found " + file.getAbsolutePath()); 
    } 
    if (!file.isDirectory()) { 
     // should be a JAR 
     file = file.getParentFile(); 
    } 
    return file; 
} 

它要麼返回一個包含類文件或包含JAR文件的文件夾的根文件夾,如果它被打包。