2013-06-20 96 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文件的文件夹的根文件夹,如果它被打包。