如何在 Java 属性文件中存储数组

问题描述 投票:0回答:10

我目前正在制作一个需要加载并转换为数组的.properties文件。
每个属性键都可能存在 0-25 个。

我尝试了一些实现,但我只是坚持干净地做这件事。
有人有什么想法吗?

foo.1.filename=foo.txt
foo.1.expire=200

foo.2.filename=foo2.txt
foo.2.expire=10

etc more foo's

bar.1.filename=bar.txt
bar.1.expire=100

我将把文件名/过期配对组装成一个数据对象,作为每个父属性元素的数组的一部分,如

foo[myobject]

属性文件的格式可以改变,我愿意接受想法。

java arrays properties-file
10个回答
46
投票

我可以建议使用分隔符和使用

String.split(delimiter)

示例属性文件:

MON=0800#Something#Something1, Something2

prop.load(new FileInputStream("\\\\Myseccretnetwork\\Project\\props.properties"));
String[]values = prop.get("MON").toString().split("#");

希望有帮助


14
投票

没有完全理解你的意图。 请检查 Apache Commons 配置库http://commons.apache.org/configuration/

你可以对一个键有多个值,如 键=值1,值2 你可以把它读入一个数组

configuration.getAsStringArray("key")


13
投票

要么定义一个不会成为潜在值的分隔符,要么学习使用 XML。

如果您仍然坚持使用属性,请使用将返回所有键列表的方法之一。您的密钥似乎包含三个部分,一个是组标识符 (foo, bar),一个是索引 (1, 2),然后是一个元素名称 (filename, expire)。获取所有密钥将它们分解成它们的组成部分。为每种类型的标识符创建一个列表,在处理列表时使用标识符来确定要添加到哪个列表。按照您所说的创建配对元素,然后简单地添加到列表中!如果索引顺序很重要,要么将其作为字段添加到配对元素中,要么在处理之前对键进行排序。


8
投票

使用 YAML 文件作为属性,这支持属性作为数组。

快速浏览 YAML:

JSON 的超集,它可以做 JSON 能做的一切以及更多

  1. 简单易读
  2. 长属性到多行值
  3. 支持评论
  4. 属性作为数组
  5. YAML 验证

6
投票

我有自定义加载。属性必须定义为:

key.0=value0
key.1=value1
...

自定义加载:

/** Return array from properties file. Array must be defined as "key.0=value0", "key.1=value1", ... */
public List<String> getSystemStringProperties(String key) {

    // result list
    List<String> result = new LinkedList<>();

    // defining variable for assignment in loop condition part
    String value;

    // next value loading defined in condition part
    for(int i = 0; (value = YOUR_PROPERTY_OBJECT.getProperty(key + "." + i)) != null; i++) {
        result.add(value);
    }

    // return
    return result;
}

4
投票

我强烈推荐使用 Apache Commons (http://commons.apache.org/configuration/)。它能够使用 XML 文件作为配置文件。使用 XML 结构可以轻松地将数组表示为值列表,而不是专门编号的属性。


3
投票

这是另一种通过自己实现机制来实现的方法。 这里我们认为数组应该从 0 开始并且索引之间没有空洞

    /**
     * get a string property's value
     * @param propKey property key
     * @param defaultValue default value if the property is not found
     * @return value
     */
    public static String getSystemStringProperty(String propKey,
            String defaultValue) {
        String strProp = System.getProperty(propKey);
        if (strProp == null) {
            strProp = defaultValue;
        }
        return strProp;
    }

    /**
     * internal recursive method to get string properties (array)
     * @param curResult current result
     * @param paramName property key prefix
     * @param i current indice
     * @return array of property's values
     */
    private static List<String> getSystemStringProperties(List<String> curResult, String paramName, int i) {
        String paramIValue = getSystemStringProperty(paramName + "." + String.valueOf(i), null);
        if (paramIValue == null) {
            return curResult;
        }
        curResult.add(paramIValue);
        return getSystemStringProperties(curResult, paramName, i+1);
    }

    /**
     * get the values from a property key prefix
     * @param paramName property key prefix
     * @return string array of values
     */
    public static String[] getSystemStringProperties(
            String paramName) {
        List<String> stringProperties = getSystemStringProperties(new ArrayList<String>(), paramName, 0);
        return stringProperties.toArray(new String[stringProperties.size()]);
    }

这里有一个测试方法:

    @Test
    public void should_be_able_to_get_array_of_properties() {
        System.setProperty("my.parameter.0", "ooO");
        System.setProperty("my.parameter.1", "oO");
        System.setProperty("my.parameter.2", "boo");
        // WHEN 
        String[] pluginParams = PropertiesHelper.getSystemStringProperties("my.parameter");

        // THEN
        assertThat(pluginParams).isNotNull();
        assertThat(pluginParams).containsExactly("ooO","oO","boo");
        System.out.println(pluginParams[0].toString());
    }

希望这有帮助

欢迎所有评论..


0
投票

正如用户“Skip Head”已经指出的那样,csv 或任何表格文件格式更适合您的情况。

如果它适合您,也许这个 Table 实现可能会让您感兴趣。


-1
投票

我建议使用属性文件,引用 CSV 文件。 然后将 CSV 文件解析为集合/数组等。 属性文件似乎不适合这种数据。


-6
投票

其实所有答案都是错的

简单:

foo.[0]filename

© www.soinside.com 2019 - 2024. All rights reserved.