如何向java.util.Properties添加说明?

如何向java.util.Properties添加说明?,java,properties,Java,Properties,我最近才发现了java.util.Properties类,并且对它非常感兴趣。由于属性只是键值对的一个简单映射,是否有一种方便的方法向它们添加一个小的描述字符串,并将其称为例如properties.get(“key”).getDescription()?否。您可以做的最接近的方法是有一个命名约定,例如 key=value key.description=value 然后使用 properties.get("key.description") 当然,您可以通过将属性包装到自己的类中来隐藏该约定

我最近才发现了
java.util.Properties
类,并且对它非常感兴趣。由于属性只是键值对的一个简单映射,是否有一种方便的方法向它们添加一个小的描述字符串,并将其称为例如
properties.get(“key”).getDescription()

否。您可以做的最接近的方法是有一个命名约定,例如

key=value
key.description=value
然后使用

properties.get("key.description")
当然,您可以通过将属性包装到自己的类中来隐藏该约定:

class Entry {
    private String value;
    private String description;

    ...
}

class Config { 
    private Properties properties;

    public Entry get(String key) {
        // get the value for key
        // get the value for key.description
        // create and return an Entry
    }
}
但是,如果您确实需要结构化数据,则应该使用JSON、YAML或XML而不是属性。

此properties.get(“key”).getDescription()不起作用,因为properties.get(“key”)返回字符串值。因此,只能对其调用与字符串相关的函数

你能做的就是吃点

key.description=value
然后使用

properties.get("key.description")