抱歉,如果标题不清楚。
我想做的是“在彼此之上”加载配置文件。
假设我有配置#1:
config.property=这里有东西
和配置#2:
config.otherproperty=Other Thingy Here
Java 应用程序加载它是这样的:
config.property=这里有东西
config.otherproperty=Other Thingy Here
就好像它是一个文件。
我该怎么做?
最佳答案
我不清楚你真正需要什么。如果我理解正确的话,您想将两个属性文件加载到一个 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}
关于Java:将多个属性文件作为一个加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26706162/