[英]Java: Load multiple properties files as one
Sorry if the title is unclear. 抱歉,标题不清楚。
What I'm trying to do is load configuration files "on top of each other". 我正在尝试做的是“彼此叠加”加载配置文件。
Say I have Config #1: 说我有配置#1:
config.property=Something Here
And Config #2: 和配置2:
config.otherproperty=Other Thingy Here
And the java app loads it as this: 然后,Java应用程序将其加载为:
config.property=Something Here
config.otherproperty=Other Thingy Here
As though it was all one file. 好像都是一个文件。
How would I do this? 我该怎么做?
Use java.util.Properties
with the defaults
constructor parameter. 将
java.util.Properties
与defaults
构造函数参数一起使用。 Whichever one should over-ride the other in the event of any conflicts should be constructed last, with the other as the defaults.
万一发生任何冲突,应优先于另一个,而另一个应作为
defaults.
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. 如果我对您的理解正确,则希望将两个属性文件加载到一个
Properties
对象中。 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!");
}
The file config1.properties contains: 文件config1.properties包含:
color=blue
And the file config2.properties contains: 并且文件config2.properties包含:
shape=circle
The snippet above outputs: 上面的输出片段:
{shape=circle, color=blue}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.