简体   繁体   English

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

[英]How to get absolute path to file in /resources folder of your project

Assume standard maven setup.假设标准 maven 设置。

Say in your resources folder you have a file abc .假设在您的资源文件夹中有一个文件abc

In Java, how can I get absolute path to the file please?在Java中,我怎样才能获得文件的绝对路径?

The proper way that actually works:实际工作的正确方法:

URL resource = YourClass.class.getResource("abc");
Paths.get(resource.toURI()).toFile();

It doesn't matter now where the file in the classpath physically is, it will be found as long as the resource is actually a file and not a JAR entry.现在类路径中的文件的物理位置并不重要,只要资源实际上是文件而不是 JAR 条目,它就会被找到。

(The seemingly obvious new File(resource.getPath()) doesn't work for all paths! The path is still URL-encoded!) (看似明显的new File(resource.getPath())不适用于所有路径!路径仍然是 URL 编码的!)

You can use ClassLoader.getResource method to get the correct resource.您可以使用ClassLoader.getResource方法来获取正确的资源。

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

OR或者

Although this may not work all the time, a simpler solution -虽然这可能不会一直有效,但一个更简单的解决方案 -

You can create a File object and use getAbsolutePath method:您可以创建一个File对象并使用getAbsolutePath方法:

File file = new File("resources/abc.txt");
String absolutePath = file.getAbsolutePath();

You need to specifie path started from /您需要指定从/开始的路径

URL resource = YourClass.class.getResource("/abc");
Paths.get(resource.toURI()).toFile();

Create the classLoader instance of the class you need, then you can access the files or resources easily.创建您需要的类的 classLoader 实例,然后您就可以轻松访问文件或资源。 now you access path using getPath() method of that class.现在您使用该类的getPath()方法访问路径。

 ClassLoader classLoader = getClass().getClassLoader();
 String path  = classLoader.getResource("chromedriver.exe").getPath();
 System.out.println(path);

To return a file or filepath返回文件或文件路径

URL resource = YourClass.class.getResource("abc");
File file = Paths.get(resource.toURI()).toFile(); // return a file
String filepath = Paths.get(resource.toURI()).toFile().getAbsolutePath();  // return file path

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);

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.至于class ,它可以通过静态或动态方式获得。


If the project is not in the plugin, the code if run in JUnit, will print:如果项目不在插件中,则代码如果在 JUnit 中运行,将打印:

loaderDir = /C:.../ws/source.dir/target/test-classes/
 localDir = /C:.../ws/source.dir/target/test-classes/package/

So, to get to src/rest/resources we should go up and down the file tree.因此,要访问 src/rest/resources,我们应该在文件树中上下移动。 Both methods can be used.这两种方法都可以使用。 Notice, we can't use getResource(resourceFolderName) , for that folder is not in the target folder.请注意,我们不能使用getResource(resourceFolderName) ,因为该文件夹不在目标文件夹中。 Nobody puts resources in the created folders, I hope.我希望没有人将资源放入创建的文件夹中。


If the class is in the package that is in the plugin, the output of the same test will be:如果该类位于插件中的包中,则相同测试的输出将是:

loaderDir = /C:.../ws/plugin/bin/
 localDir = /C:.../ws/plugin/bin/package/

So, again we should go up and down the folder tree.因此,我们应该再次在文件夹树中上下移动。


The most interesting is the case when the package is launched in the plugin.最有趣的是在插件中启动包的情况。 As JUnit plugin test, for our example.作为 JUnit 插件测试,对于我们的示例。 The output is:输出是:

loaderDir = /C:.../ws/plugin/
 localDir = /package/

Here we can get the absolute path only combining the results of both functions.在这里我们可以得到仅结合两个函数的结果的绝对路径。 And it is not enough.这还不够。 Between them we should put the local path of the place where the classes packages are, relatively to the plugin folder.在它们之间,我们应该放置类包所在位置的本地路径,相对于插件文件夹。 Probably, you will have to insert something as src or src/test/resource here.可能,您必须在此处插入一些内容作为srcsrc/test/resource

You can insert the code into yours and see the paths that you have.您可以将代码插入您的代码并查看您拥有的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得绝对路径<project> \\目标文件夹 - How get absolute path to <project>\taget folder JAVA项目中如何获取文件绝对路径 - How to get the file absolute path in JAVA project 如何获取项目中文件的绝对路径(而不是存储在临时文件夹中的文件的绝对路径) - How to get the absolute path of a file in my project (and not of a file stored in a temporary folder) 使用getResourceAsStream()在项目文件夹之外的文件的绝对路径 - Absolute path to file outside of project folder with getResourceAsStream() 如何在Java中获取资源目录的绝对路径? - How to get the absolute path for the resources directory in Java? Spring项目如何设置真实和绝对路径来获取属性文件? - Spring project how to set realtive and absolute path to get a properties file? 如何获取.bat文件中使用的eclipse项目的绝对路径? - How to get the absolute path of eclipse project to use in .bat file? Java-如何获取项目中文件的正确绝对路径 - Java - How to get the correct absolute path of a file within a project maven项目部署后如何获取正确的绝对文件路径 - How to get the correct absolute file path after maven project is deployed 如何获取 src/main/resources 文件夹中文件的路径? - How to get the path to a file in src/main/resources folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM