Java:将多个属性文件作为一个加载

Noo*_*dly 1 java file

如果标题不清楚,请见谅。

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

假设我有配置 #1:

config.property=Something Here

和配置#2:

config.otherproperty=Other Thingy Here

Java 应用程序将其加载为:

config.property=Something Here

config.otherproperty=Other Thingy Here

好像这都是一个文件。

我该怎么做?

hfo*_*nez 5

我不清楚你真正需要什么。如果我理解正确,您想将两个属性文件加载到一个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!");
}
Run Code Online (Sandbox Code Playgroud)

文件config1.properties包含:

color=blue
Run Code Online (Sandbox Code Playgroud)

文件config2.properties包含:

shape=circle
Run Code Online (Sandbox Code Playgroud)

上面的代码片段输出:

{shape=circle, color=blue}
Run Code Online (Sandbox Code Playgroud)