java如何定义文件的相对路径?
我和cucumber和maven有一个项目,来自Intellij IDEA
我需要使用相对文件路径。现在我正在mac os上工作,但该项目也将在windows上使用
- 如何从RunnerTest确定相对路径。java应用程序。mac os的属性文件李>
- 为Windows定义相同的路径会有什么不同吗李>
这是我的跑步测试。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);
}
}
}
以下是我的项目结构图(为清晰起见,添加了突出显示):
我试着用这种方法。但这没有帮助
1. "../../config/application.properties"
2. "src/resources/config/application.properties"
3. "../../resources/config/application.properties"
# 1 楼答案
我并不假装是正确的,但它起了作用:
src -> main
中创建了resources
文件夹,并将config
文件夹移到了那里。我在src -> test
中创建了resources
文件夹,并将所有其他文件夹移到了那里。结果就是这样一种结构:System.setProperty("TagConfigFile", "../../config/application.properties");
我在RunnerTest.java
课上写了System.setProperty("TagConfigFile", "./config/application.properties");
如果有人提供一个更漂亮的工作方案,我会非常高兴