如何定义文件的相对路径?
How define relative path to file?
我有一个使用 Cucumber 和 maven 的项目,来自 Intellij IDEA。
我需要使用相对文件路径。现在我正在 mac os 上工作,但该项目也将在 windows 上使用。
- 如何确定 RunnerTest.java 到 application.properties 文件的相对路径 mac os?
- 为Windows定义相同的路径会有什么不同吗?
这是我的RunnerTest.JavaClass
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);
}
}
}
这是我的项目结构图(为清楚起见,添加了突出显示):
我尝试过这样的方式。但这并没有帮助
1. "../../config/application.properties"
2. "src/resources/config/application.properties"
3. "../../resources/config/application.properties"
我没有假装正确,但确实有效:
- 我在
src -> main
中创建了文件夹 resources
并将文件夹 config
移到了那里。我在 src -> test
中创建了文件夹 resources
,并将所有其他文件夹移到了那里。结果是这样的结构:
src
|----main
| |
| |----java
| |----resources
| | |----config
| | | |----application.properties
|
|----test
| |----resources
| | |----features
| | |----pipes
| | |----testdata
| | |----webdrivers
- 而是
System.setProperty("TagConfigFile", "../../config/application.properties");
我在 RunnerTest.java
class 中写了 System.setProperty("TagConfigFile", "./config/application.properties");
如果有人提供更漂亮的工作方案,我会很高兴
我有一个使用 Cucumber 和 maven 的项目,来自 Intellij IDEA。
我需要使用相对文件路径。现在我正在 mac os 上工作,但该项目也将在 windows 上使用。
- 如何确定 RunnerTest.java 到 application.properties 文件的相对路径 mac os?
- 为Windows定义相同的路径会有什么不同吗?
这是我的RunnerTest.JavaClass
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);
}
}
}
这是我的项目结构图(为清楚起见,添加了突出显示):
我尝试过这样的方式。但这并没有帮助
1. "../../config/application.properties"
2. "src/resources/config/application.properties"
3. "../../resources/config/application.properties"
我没有假装正确,但确实有效:
- 我在
src -> main
中创建了文件夹resources
并将文件夹config
移到了那里。我在src -> test
中创建了文件夹resources
,并将所有其他文件夹移到了那里。结果是这样的结构:
src
|----main
| |
| |----java
| |----resources
| | |----config
| | | |----application.properties
|
|----test
| |----resources
| | |----features
| | |----pipes
| | |----testdata
| | |----webdrivers
- 而是
System.setProperty("TagConfigFile", "../../config/application.properties");
我在RunnerTest.java
class 中写了
System.setProperty("TagConfigFile", "./config/application.properties");
如果有人提供更漂亮的工作方案,我会很高兴