Java:将多个属性文件作为一个加载 [英] Java: Load multiple properties files as one
问题描述
对不起,标题不清楚.
我要做的是彼此叠加"加载配置文件.
What I'm trying to do is load configuration files "on top of each other".
说我有配置#1:
config.property=Something Here
和配置2:
config.otherproperty=Other Thingy Here
然后Java应用程序将其加载为:
And the java app loads it as this:
config.property=Something Here
config.otherproperty=Other Thingy Here
好像都是一个文件.
我该怎么做?
推荐答案
我不清楚您真正需要什么.如果我对您的理解正确,则要将两个属性文件加载到单个Properties
对象中.如果是这种情况,您要做的就是这样:
I am unclear as to what you really need. If I understood you correctly, you want to load two properties file into a single Properties
object. If this is the case, the all you have to do is something like this:
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 包含:
The file config1.properties contains:
color=blue
文件 config2.properties 包含:
And the file config2.properties contains:
shape=circle
上面的输出片段:
{shape=circle, color=blue}
这篇关于Java:将多个属性文件作为一个加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!