Graham
Graham

Reputation: 33

Reading multiple java property values into string or array

Hopefully someone is able to help, I'm relatively new at java and trying to work out how to use the properties function to read multiple property values, not necessarily in order or the full list and then put them into either an array or a string so that I can then pass to another class to do "stuff" like write to a file. There could potentially be hundreds of property values and only wanted to pick the ones I wanted. I'm able to get one like properties.getProperty("ip"); and assign to a string but having issues with multiple as per below...

Any help would be greatly appreciated.

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")];

If you need additional info or data samples happy to supply. Cheers and thanks in advance :)

Upvotes: 0

Views: 1502

Answers (1)

Adit A. Pillai
Adit A. Pillai

Reputation: 667

If you know the "keys" to the properties, you can use an ArrayList of strings to store the properties.

for example:

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

Here I'm assuming that you do not know how many keys are you going to pick up from the properties and hence the suggestion to use an ArrayList, but if you do know the number of keys to be picked, you should definitely use an array of strings.

for example:

String[] propertyArray = new String[limit];

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

here, "myKey" can be coded to change dynamically.

Upvotes: 1

Related Questions