如果标题不清楚,请抱歉。
我想要做的是加载配置文件"在彼此之上"。
说我有Config#1:
config.property=Something Here
和配置#2:
config.otherproperty=Other Thingy Here
java应用程序将其加载为:
config.property=Something Here
config.otherproperty=Other Thingy Here
好像它只是一个文件。
我该怎么做?
答案 0 :(得分:3)
将java.util.Properties
与defaults
构造函数参数一起使用。如果出现任何冲突,任何人都应该覆盖另一个,最后应该构建,另一个作为defaults.
答案 1 :(得分:3)
我不清楚你真正需要什么。如果我理解正确,您希望将两个属性文件加载到单个Properties
对象中。如果是这种情况,你所要做的就是这样:
PropsDemo demo = new PropsDemo();
String prop1 = "config1.properties";
String prop2 = "config2.properties";
Properties props = new Properties();
InputStream input1 = demo.getClass().getClassLoader().getResourceAsStream(prop1);
InputStream input2 = demo.getClass().getClassLoader().getResourceAsStream(prop2);
try
{
props.load(input1);
props.load(input2);
System.out.println(props.toString());
}
catch (IOException e)
{
System.out.println("Something went wrong!");
}
config1.properties 文件包含:
color=blue
文件 config2.properties 包含:
shape=circle
上面的代码段输出:
{shape=circle, color=blue}