【发布时间】:2017-07-27 16:07:32
【问题描述】:
有趣的是,我发现了两个标题相似的几乎相同的问题:
但我的情况似乎略有不同。
所以我有一个 config.properties 文件正在由 ConfigurationGetter 类加载,如下所示:
public class ConfigurationGetter {
Properties prop = new Properties();
InputStream inputStream = null;
public Properties getPropValues() throws IOException {
try {
String fileName = "config.properties";
inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
}
} catch (Exception e) {
System.out.println("Exception: " + e);
} finally {
inputStream.close();
}
return prop;
}
然后我在另一个类中实例化这个类,如下所示:
ConfigurationGetter config = new ConfigurationGetter();
props = config.getPropValues();
然后我可以像这样按键提取属性:
props.getProperty("keyName")
如果我通过命令行提供它,我想做的是覆盖我从属性文件中获得的这个值。例如,如果我在我的config.properties 中有这样一行,我按上述说明加载:
keyName=true
我也像这样运行代码:
mvn test -DkeyName=false
那么false 将是解决的问题。
【问题讨论】:
标签: java command-line properties