2014-11-03 106 views
1

对不起,如果标题不清楚。Java:将多个属性文件作为一个加载

我想要做的是加载配置文件“在彼此之上”。

说我有配置#1:

config.property=Something Here

和配置#2:

config.otherproperty=Other Thingy Here

和Java应用程序加载它是这样的:

config.property=Something Here

config.otherproperty=Other Thingy Here

就像它是一个文件一样。

我该怎么做?

回答

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} 
3

使用java.util.Propertiesdefaults构造函数参数。如果发生任何冲突,应该建立最后一个,另一个作为defaults.

+0

对不起,我还不清楚。假设我有两个文本文件,一个是“hello world”,另一个是“hello nobody”。我怎样才能让我的应用程序读取'hello world hello nobody'好像它是一个文件? – 2014-11-03 02:04:11