2014-11-03 104 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