2

我想将上传的文件存储在我的项目文件夹中,那么如何获取 java swing 项目的项目路径

4

2 回答 2

3

我用这个得到了它......

System.getProperty("user.dir")
于 2013-06-20T06:26:46.233 回答
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 文件的文件夹(如果已打包)。

于 2013-06-20T06:34:45.390 回答