【发布时间】:2017-03-29 04:49:50
【问题描述】:
我使用 gradle,它以 maven 风格构建项目,所以我有以下内容
src/main/java/Hello.java 和 src/main/resources/test.properties
我的Hello.java 看起来像这样
public class Hello {
public static void main(String[] args) {
Properties configProperties = new Properties();
ClassLoader classLoader = Hello.class.getClassLoader();
try {
configProperties.load(classLoader.getResourceAsStream("test.properties"));
System.out.println(configProperties.getProperty("first") + " " + configProperties.getProperty("last"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
这很好用。但是我希望能够在我的项目之外指向.properties 文件,并且我希望它足够灵活,以便我可以指向任何位置无需每次都重建 jar。有没有办法不使用 File API 并将文件路径作为参数传递给 main 方法?
【问题讨论】: