如何确定Maven项目中的文件路径?(How to determine a file path in Maven project?)
java
我正在使用Maven进行项目。 在我的一个java类中,我需要获取一个属性文件。 我不能使用绝对文件位置而是相对路径。 我尝试了几种方法,但没有运气。
java在
myProject/src/main/java/com/some/myConfiguration.java属性文件位于
myProject/src/main/com/some/resources/myProperties/myFeatures.properties在我的java中,我有一些像这样的代码
String filePath = "correct/path/to/myFeatures.properties"; File repositoryFile = new File(filePath);所以我的问题是我应该给filePath
谢谢你的任何建议。
I am using Maven for a project. In one of my java class, I need to grab a property file. I cannot use absolute file location but relative path. I tried several ways but no luck.
The java is in
myProject/src/main/java/com/some/myConfiguration.javaThe property file is in
myProject/src/main/com/some/resources/myProperties/myFeatures.propertiesAnd in my java, I have some code like this
String filePath = "correct/path/to/myFeatures.properties"; File repositoryFile = new File(filePath);So my problem is what I should give to filePath
Thank you for any advice.
最满意答案
将myFeatures.properties属性文件放在myFeatures.properties src/main/resources如Maven的标准目录布局所示使用以下命令在Java代码中访问它:
this.getClass().getClassLoader().getResourceAsStream("myFeatures.properties") ;
put your myFeatures.properties property file inside src/main/resources as suggested by Maven's Standard Directory Layoutaccess it in your Java code using:
this.getClass().getClassLoader().getResourceAsStream("myFeatures.properties");
更多推荐
发布评论