从单个文件加载多个 class 实例的多个属性集

Loading multiple properties sets from single file for multiple class instances

我有一个 class 如果其中一个属性发生变化,我需要一个不同的实例。这些更改在运行时从 属性 文件中读取。 我想要一个文件来详细说明所有单个实例的属性:

------------
name=Milan
surface=....
------------
name=Naples
surface=....

如何在不同的 属性 class 中加载每组属性(也许创建 Properties[])?是否有 Java 内置方法来执行此操作? 我是否应该手动解析它,如何在集合中找到分割字符串时随时创建一个 InputStream?

ArrayList<Properties> properties = new ArrayList<>();
if( whateverItIs.nextLine() == "----" ){
        InputStream limitedInputStream = next-5-lines ;
        properties.add(new Properties().load(limitedInputStream));
}

类似上面的内容。而且,顺便说一下,任何直接从文件创建 class 的构造方法?

编辑:任何指向正确方向的东西我自己也可以。

首先,将整个文件作为一个字符串读取。然后使用 splitStringReader.

String propertiesFile = FileUtils.readFileToString(file, "utf-8");
String[] propertyDivs = propertiesFile.split("----");
ArrayList<Properties> properties = new ArrayList<Properties>();

for (String propertyDiv : propertyDivs) {
     properties.add(new Properties().load(new StringReader(propertyDiv)));
}

上面的例子使用apache commons-io库文件到String一行,因为Java没有这样的内置方法。但是,可以使用标准 Java 库轻松实现读取文件,请参阅 Whole text file to a String in Java