[英]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:我们在通往绝对路径的路上有两个问题:
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.可能,您必须在此处插入一些内容作为
src
或src/test/resource
。
You can insert the code into yours and see the paths that you have.您可以将代码插入您的代码并查看您拥有的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.