如何在项目的/ resources文件夹中获取文件的绝对路径

Jam*_*sev 71 java resources file maven

假设标准maven设置.

在资源文件夹中说你有一个文件abc.

在Java中,我如何获得文件的绝对路径?

Kar*_*l S 148

实际工作的正确方法:

URL resource = YourClass.class.getResource("abc");
Paths.get(resource.toURI()).toFile();
Run Code Online (Sandbox Code Playgroud)

现在,类路径中的文件在物理上的位置并不重要,只要资源实际上是文件而不是JAR条目,就会找到它.

(看似明显new File(resource.getPath())不适用于所有路径!路径仍然是URL编码的!)

  • 谢谢!这几乎对我有用.但我不得不做一个改变:YourClass.class.getClassLoader().getResource("abc"); (25认同)
  • 或者大概你可以这样做:`new File(resource.toURI()).getAbsolutePath();`(即我认为你不需要Path对象?) (5认同)
  • 我不明白这对没有斜线的很多人是如何起作用的:`.getResource("/abc")` (3认同)
  • 关于 toURI() 的好提示,这可以避免路径中出现 %20 的空格! (2认同)

Rah*_*ate 41

您可以使用ClassLoader.getResource方法来获取正确的资源.

URL res = getClass().getClassLoader().getResource("abc.txt");
File file = Paths.get(res.toURI()).toFile();
String absolutePath = file.getAbsolutePath();
Run Code Online (Sandbox Code Playgroud)

要么

虽然这可能不会一直有效,但更简单的解决方案 -

您可以创建一个File对象并使用getAbsolutePath方法:

File file = new File("resources/abc.txt");
String absolutePath = file.getAbsolutePath();
Run Code Online (Sandbox Code Playgroud)

  • 有限使用的建议,因为它依赖于工作目录作为maven root.即便如此,您还是应该使用`target/classes/abc.txt`来引用该文件,因为这是Maven在处理后放置资源文件的规范位置(例如,maven-resources插件可能已经完成了属性替换)的abc.txt).通过类路径中的getResource()来使用abc.txt要好得多. (7认同)
  • 这可以作为正确答案删除吗?@Karol S回答如下 - 这应该是正确的答案(因此upvote差异) (6认同)
  • 错误的答案.请参考以下@Karol的回答 (3认同)
  • 如果最终用户将您的应用程序作为可执行JAR运行,该怎么办?然后根本就没有物理文件.这是你应该使用getResource()的另一个原因,例如打开输入流,这取决于你想用它做什么. (2认同)

Het*_*ett 21

您必须指定从中开始的路径 /

URL resource = YourClass.class.getResource("/abc");
Paths.get(resource.toURI()).toFile();
Run Code Online (Sandbox Code Playgroud)


小智 11

创建所需类的classLoader实例,然后您可以轻松地访问文件或资源.现在您使用getPath()该类的方法访问路径.

 ClassLoader classLoader = getClass().getClassLoader();
 String path  = classLoader.getResource("chromedriver.exe").getPath();
 System.out.println(path);
Run Code Online (Sandbox Code Playgroud)


Gan*_*nus 5

There are two problems on our way to the absolute path:

  1. The placement found will be not where the source files lie, but where the class is saved. And the resource folder almost surely will lie somewhere in the source folder of the project.
  2. The same functions for retrieving the resource work differently if the class runs in a plugin or in a package directly in the workspace.

The following code will give us all useful paths:

    URL localPackage = this.getClass().getResource("");
    URL urlLoader = YourClassName.class.getProtectionDomain().getCodeSource().getLocation();
    String localDir = localPackage.getPath();
    String loaderDir = urlLoader.getPath();
    System.out.printf("loaderDir = %s\n localDir = %s\n", loaderDir, localDir);
Run Code Online (Sandbox Code Playgroud)

Here both functions that can be used for localization of the resource folder are researched. As for class, it can be got in either way, statically or dynamically.


If the project is not in the plugin, the code if run in JUnit, will print:

loaderDir = /C:.../ws/source.dir/target/test-classes/
 localDir = /C:.../ws/source.dir/target/test-classes/package/
Run Code Online (Sandbox Code Playgroud)

因此,要到达 src/rest/resources,我们应该在文件树中上下移动。两种方法都可以使用。请注意,我们不能使用getResource(resourceFolderName),因为该文件夹不在目标文件夹中。我希望没有人将资源放入创建的文件夹中。


如果该类位于插件的包中,则相同测试的输出将是:

loaderDir = /C:.../ws/plugin/bin/
 localDir = /C:.../ws/plugin/bin/package/
Run Code Online (Sandbox Code Playgroud)

因此,我们应该再次在文件夹树中上下移动。


最有趣的是在插件中启动包时的情况。作为 JUnit 插件测试,以我们为例。输出是:

loaderDir = /C:.../ws/plugin/
 localDir = /package/
Run Code Online (Sandbox Code Playgroud)

这里我们只需结合两个函数的结果就可以得到绝对路径。但这还不够。在它们之间,我们应该放置类包所在位置的本地路径(相对于插件文件夹)。也许,您必须在此处插入一些内容 assrcsrc/test/resource

您可以将代码插入到您的代码中并查看您拥有的路径。