如何确定Maven项目中的文件路径?(How to determine a file path in Maven project?)

编程入门 行业动态 更新时间:2024-10-28 12:26:01
如何确定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.java

The property file is in

myProject/src/main/com/some/resources/myProperties/myFeatures.properties

And 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 Layout

access it in your Java code using:

this.getClass().getClassLoader().getResourceAsStream("myFeatures.properties");

更多推荐

本文发布于:2023-08-06 02:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442104.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   文件   项目   Maven   project

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!