Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading different set of properties in java to an ArrayList

Tags:

java

java-io

I have a list of properties like this,

server1.serverName =""
server1.serverType ="'
server1.hostName =""
server1.userName =""
server1.password =""

in a property file and I have 'n' no of sets,like

server2, server3,...servern in a property file. And also I have a class which contain all off these elements with getter and setter method,

public class ServerDetails implements Serializable {

  private String serverName;
  private String serverType;
  private String hostName;
  private String userName;
  private String password;
  ...
}

Now, I need to read the above property file and create an arraylist like ,

ArrayList<ServerDetails> serverDetailsList = new ArrayList<ServerDetails>();

where each element of the arrayList should have an object of the class ServerDetails. I need to know how to read the property file and get the server details so that I can create an object and add it to the list.

It seems kind of easy but i lost my way. Kindly help.

Thank you !!

Regards, Bala

like image 549
sbala_20 Avatar asked Dec 11 '25 05:12

sbala_20


1 Answers

You could load the properties file into a Properties object, then loop through the properties like so:

int i = 1;
while( properties.get( "server" + i + ".serverName" ) != null ) {
    ServerDetails details = new ...
    details.setServerName( properties.get( [as above] ) );
    ...
    list.add( details );
    ++i;
}
like image 173
Ray Stojonic Avatar answered Dec 12 '25 17:12

Ray Stojonic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!