如何定义文件的相对路径?

问题描述 投票:1回答:1

我有一个包含黄瓜和maven的项目,来自Intellij IDEA。

我需要使用相对文件路径。现在,我正在Mac OS上工作,但是该项目也将在Windows上使用。

  1. 如何确定从RunnerTest.java到Mac OS的application.properties文件的相对路径?
  2. 为Windows定义相同路径会不会有任何区别?

这是我的RunnerTest.Java类

public class RunnerTest {

    public static void main(String[] args) throws Throwable {
        System.setProperty("TagConfigFile", "../../config/application.properties");

        //...

        String sConfigFile = System.getProperty("TagConfigFile", "null");

        try (InputStream streamFromResources = Props.class.getClassLoader().getResourceAsStream(sConfigFile)) {
            /* work wiht  streamFromResources */
        } catch (IOException | NullPointerException ee) {
            throw new PropsRuntimeException("Failed to access properties file", ee);
        }
    }
}

这是我的项目结构的图像(为清楚起见,添加了突出显示):

enter image description here

我试图使用这种方式。但这没有帮助

 1. "../../config/application.properties"
 2. "src/resources/config/application.properties"
 3. "../../resources/config/application.properties"
java path cucumber filepath
1个回答
0
投票

我不假装是正确的,但是有效:

  1. 我在resources中创建了文件夹src -> main,并将文件夹config移到了那里。我在resources中创建了文件夹src -> test,并将所有其他文件夹移到了那里。结果是这样的结构:
src
 |----main
 |      |
 |      |----java
 |      |----resources
 |      |        |----config
 |      |        |       |----application.properties
 |
 |----test
 |      |----resources
 |      |        |----features
 |      |        |----pipes
 |      |        |----testdata
 |      |        |----webdrivers
  1. 代替System.setProperty("TagConfigFile", "../../config/application.properties");我在System.setProperty("TagConfigFile", "./config/application.properties");类中写了RunnerTest.java

如果有人提供更漂亮的工作解决方案,我将非常高兴

© www.soinside.com 2019 - 2024. All rights reserved.