簡體   English   中英

如何在java.util.Properties中添加描述?

[英]How to add descriptions to java.util.Properties?

我最近才發現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”)返回一個String值。 因此,您只能在其上調用與String相關的函數。

你能做的就是

key.description=value

然后使用

properties.get("key.description") 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM