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

标签 java file

抱歉,如果标题不清楚。

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

假设我有配置#1:

config.property=这里有东西

和配置#2:

config.otherproperty=Other Thingy Here

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

config.property=这里有东西

config.otherproperty=Other Thingy Here

就好像它是一个文件。

我该怎么做?

最佳答案

我不清楚你真正需要什么。如果我理解正确的话,您想将两个属性文件加载到一个 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}

关于Java:将多个属性文件作为一个加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26706162/

相关文章:

java - 从 getItem() 制作对象并访问字段

java - 异常 : 401 Unauthorized Error | com. google.api.client.auth.oauth2.TokenResponseException:401 未经授权

java - 构造函数不读取 int[]

file - Lua 保存字符串到文件

c 使用 lseek 以相反的顺序复制文件

java - 无法保存扩展名为 .json 的文件

java - Artifact 部署 Tomcat 9 期间的应用程序错误

java - 如何使用Hadoop将CQL集合对象保存到Cassandra?

c - 二进制到文本错误更正

c - 使用 read() 函数读取文件