简体   繁体   中英

How to add descriptions to java.util.Properties?

I've only recently discovered the java.util.Properties class and grew quite fond of it. Since properties are only a simple mapping of key-value pairs, is there a convenient way of adding a little description String to them and call it for example like properties.get("key").getDescription() ?

No. The closest you can do is to have a naming convention such as

key=value
key.description=value

and then use

properties.get("key.description")

You can of course hide that convention by wrapping the properties into your own class:

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

But if you really want structured data, you should use JSON, YAML or XML rather than properties.

This properties.get("key").getDescription() would not work, since properties.get("key") returns a String value. So you can only call the String related functions on it.

What you can do is have something like

key.description=value

And then call it using

properties.get("key.description") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM