在java属性文件中表示数组的更好方法

时间:2011-08-10 17:56:02

标签: java arrays enums properties

我目前正在创建一个需要加载并转换为数组的.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]

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

9 个答案:

答案 0 :(得分:35)

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

String.split(delimiter)

示例属性文件:

MON = 0800#Something#Something1,Something2

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

希望有所帮助

答案 1 :(得分:12)

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

您可以针对某个键设置多个值,例如 键=值1,值 你可以将它作为configuration.getAsStringArray("key")

读入数组

答案 2 :(得分:10)

定义一个不是潜在值的分隔符或学习使用XML。

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

答案 3 :(得分:4)

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

答案 4 :(得分:3)

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

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;
}

答案 5 :(得分:3)

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

快速浏览一下YAML:

  

JSON的超集,它可以完成JSON所能做的所有事情+更多

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

答案 6 :(得分:1)

这是通过自己实现机制的另一种方法。 这里我们认为数组应该从0开始,并且在indice

之间没有漏洞
    /**
     * 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());
    }

希望这会有所帮助

欢迎所有评论。

答案 7 :(得分:1)

实际上所有答案都是错误的

简单:foo.[0]filename

答案 8 :(得分:0)

正如用户'Skip Head'已经指出的那样,csv或任何表文件格式在你的情况下会更好。

如果它是您的选项,也许这个Table实现可能会让您感兴趣。