简体   繁体   English

如何读取.properties文件中的当前项目路径

[英]How to read current project path in .properties file

I have abc.jks file under resources folder and i need to read that abc.jks file in test.properties file. 我在resources文件夹下有abc.jks文件,我需要在test.properties文件中读取该abc.jks文件。 How to read abc.jks file in test.properties file. 如何在test.properties文件中读取abc.jks文件。

In application.properties (or how you name it) assuming you use maven and you execute your spring application in the default project you can refer to the file like: 假设您使用maven并在默认项目中执行spring应用程序,则在application.properties(或命名方式)中,您可以引用以下文件:

my.file.name = ./src/main/resources/abs.jks

or if this is for test purpose only you can put in the test resources and refer like this: 或者,如果仅出于测试目的,则可以放入测试资源并像这样引用:

my.file.name = ./src/test/resources/abs.jks

For me the best is to not be dependent on folder and always use classloader resource, but if you have to refer a path on the filesystem, from java code you can do this: 对我来说,最好的办法是不依赖文件夹,并且始终使用类加载器资源,但是如果必须在文件系统上引用路径,则可以从Java代码中执行以下操作:

URL url = getClass().getClassLoader().getResource("abs.jks");
File file = Paths.get(url.toURI()).toFile().getAbsolutePath();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM