简体   繁体   English

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

[英]How define relative path to file?

I have a project with cucumber and maven, work from Intellij IDEA.我有一个黄瓜和 maven 项目,来自 Intellij IDEA。

I need to use a relative file path.我需要使用相对文件路径。 Now I am working on mac os, but then the project will also be used on windows.现在我在 mac os 上工作,但是该项目也将在 windows 上使用。

  1. How do I determine the relative path from RunnerTest.java to application.properties file for mac os?如何确定从 RunnerTest.java 到 mac os 的 application.properties 文件的相对路径?
  2. Will there be any differences in defining the same path for Windows?为 Windows 定义相同的路径会有什么不同吗?

Here is my RunnerTest.Java Class这是我的 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);
        }
    }
}

Here is the image of my project structure (for clarity, added highlighting):这是我的项目结构的图像(为清晰起见,添加了突出显示):

在此处输入图片说明

I tried to use such ways.我尝试使用这样的方式。 But it did not help但它没有帮助

 1. "../../config/application.properties"
 2. "src/resources/config/application.properties"
 3. "../../resources/config/application.properties"

I do not pretend to be correct, but it worked:我不假装是正确的,但它有效:

  1. I created the folder resources in src -> main and moved the folder config there.我在src -> main创建了文件夹resources并将文件夹config移到那里。 I created the folder resources in src -> test and moved the all other folders there.我在src -> test创建了文件夹resources ,并将所有其他文件夹移到那里。 The result is such a structure:结果是这样的结构:
src
 |----main
 |      |
 |      |----java
 |      |----resources
 |      |        |----config
 |      |        |       |----application.properties
 |
 |----test
 |      |----resources
 |      |        |----features
 |      |        |----pipes
 |      |        |----testdata
 |      |        |----webdrivers
  1. Instead System.setProperty("TagConfigFile", "../../config/application.properties");而是System.setProperty("TagConfigFile", "../../config/application.properties"); I wrote System.setProperty("TagConfigFile", "./config/application.properties");我写了System.setProperty("TagConfigFile", "./config/application.properties"); in RunnerTest.java classRunnerTest.java类中

If someone offers a more beautiful working solution, I will be very glad如果有人提供更漂亮的工作解决方案,我会很高兴

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

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