簡體   English   中英

將多個Java屬性值讀入字符串或數組

[英]Reading multiple java property values into string or array

希望有人能夠提供幫助,我在Java方面相對較新,並且試圖弄清楚如何使用properties函數讀取多個屬性值,不一定按順序或完整列表,然后將它們放入數組或字符串中這樣我就可以傳遞給另一個類來做“工作”,例如寫文件。 可能有數百個屬性值,只想選擇我想要的值。 我可以得到像properties.getProperty(“ ip”);這樣的一個。 並分配給一個字符串,但存在如下多個問題...

任何幫助將不勝感激。

Properties properties = new Properties();

try {
    properties.load(new FileInputStream(args[0]));
} 
catch (IOException e) {
    System.out.println("Error - IOException - File not found...");
}

String model = properties.getProperty("model"); 
String codeLevel = properties.getProperty("codeLvl");

String[] dmdCommand = new String[properties.getProperty("ip")
        + properties.getProperty("rangeS")
        + properties.getProperty("rangeL")
        + properties.getProperty("PhyPG")
        + properties.getProperty("PhyLDEV")
        + properties.getProperty("PhyProc")
        + properties.getProperty("PhyExG")
        + properties.getProperty("PhyExLDEV")
        + properties.getProperty("PhyCMPK")];

如果您需要其他信息或數據樣本,歡迎提供。 預先加油並感謝:)

如果知道屬性的“鍵”,則可以使用字符串的ArrayList來存儲屬性。

例如:

List<String> propertyList = new ArrayList<String>();
propertyList.add(properties.getProperty("rangeS"));

在這里,我假設您不知道要從屬性中選擇多少個鍵,因此不建議使用ArrayList ,但是如果您知道要選擇的鍵數,則絕對應該使用數組的字符串。

例如:

String[] propertyArray = new String[limit];

for(int i=0;i<limit;i++){
    propertyArray[i]= new String(properties.getProperty(myKey));
}

在這里,“ myKey”可以被編碼為動態更改。

暫無
暫無

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

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