如何在Java文件中指定相对文件路径,以便在将文件放入jar文件后它仍然可以工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6447162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:01:35  来源:igfitidea点击:

How to specify relative file path in Java file so that it can still work after the file is put in jar file?

javajar

提问by ace

Suppose I have a Java class that needs to access a file with absolute path /home/gem/projects/bar/resources/test.csv:

假设我有一个 Java 类需要访问具有绝对路径 /home/gem/projects/bar/resources/test.csv 的文件:

package com.example
class Foo {
String filePath = ????? // path to test.csv
String lines = FileInputStream(new File(filePath).readAllLines();

}

Where the path to Foo.java is /home/gem/projects/bar/src/com/example.

Foo.java 的路径是/home/gem/projects/bar/src/com/example。

Of course I cannot specify absolute path to the resource file. This is because jar file will be distributed as library for any clients to use in their own environments.

当然我不能指定资源文件的绝对路径。这是因为 jar 文件将作为库分发给任何客户端在他们自己的环境中使用。

Assume the resource file like test.csv is always in the same path relative to project root. When a jar is created containing Foo.class, this jar also contains test.csv in the same relative path ( relative to project root).

假设像 test.csv 这样的资源文件总是在相对于项目根目录的相同路径中。当创建一个包含 Foo.class 的 jar 时,这个 jar 也在相同的相对路径(相对于项目根目录)中包含 test.csv。

What is the way to specify relative path that would work no matter where the project bar is moved to? Also how can I create a jar file (which can be in any location) so that the path to the resource file test.csv would still be correct.

无论项目栏移动到哪里,指定相对路径都可以使用的方法是什么?另外我如何创建一个 jar 文件(可以在任何位置),以便资源文件 test.csv 的路径仍然是正确的。

To keep things simple, I have used invalid Java API ( readAllLines() which reads all the lines and return a string containing entire file content. Also not using try/catch).

为简单起见,我使用了无效的 Java API( readAllLines() 读取所有行并返回包含整个文件内容的字符串。也不使用 try/catch)。

Assume csv file can be read as well as written to.

假设可以读取和写入 csv 文件。

I hope this makes it clear now.

我希望这现在清楚了。

回答by trashgod

Use getResource(), as shown here.

使用getResource(),如图所示这里

回答by Michael Borgwardt

Put the test.csvfile into the srcfolder and use this:

test.csv文件放入src文件夹并使用:

Foo.class.getResourceAsStream("/test.csv")

To get an InputStreamfor the file. This will work wherever the project is moved, including packaged as a JAR file.

获取InputStream文件的 。无论项目移动到哪里,这都将起作用,包括打包为 JAR 文件。

回答by Yahia Musse

Example:

例子:

ProjectX\src\Test.java

ProjectX\src\Test.java

ProjectX\resources\config.properties

ProjectX\res​​ources\config.properties

If you have the above structure and you want to use your config.properties file, this is how you do it:

如果你有上面的结构并且你想使用你的 config.properties 文件,你可以这样做:

InputStream input = new FileInputStream("./resources/config.projects");

InputStream input = new FileInputStream("./resources/config.projects");

In this example you don't have to worry about packaging your source into jar file. You can still modify your resources folder anytime.

在此示例中,您不必担心将源代码打包到 jar 文件中。您仍然可以随时修改您的资源文件夹。