3

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

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
sbala_20
  • 95
  • 2
  • 4
  • 10

4 Answers4

1
  1. Open the file using Java IO
  2. Parse the file line by line. Try storing the 'Key' 'value' pairs in a hashMap, you can then iterate over that adding/updating serverDetails that are stored in your arrayList.
Rylander
  • 19,449
  • 25
  • 93
  • 144
  • As noted by the other solutions you could leverage the Java Properties object to do the IO/Parsing for you. You will still need to iterate over the values and build your `serverDetails`. – Rylander Sep 23 '13 at 16:43
1

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;
}
Ray Stojonic
  • 1,260
  • 1
  • 7
  • 11
0

Quick example:

private static final String KEY ="server";
public static void main(String[] args) throws IOException {
    Properties properties = new Properties();
    properties.load(new FileInputStream("props.properties"));
    int i = 1;
    while (properties.containsKey(KEY + i + ".serverName")) {
        String serverName = properties.getProperty(KEY + i + ".serverName");
        String serverType = properties.getProperty(KEY + i + ".serverType");
        String hostName = properties.getProperty(KEY + i + ".hostName");
        String userName = properties.getProperty(KEY + i + ".userName");
        String password = properties.getProperty(KEY + i + ".password");
        System.out.println(serverName);
        System.out.println(serverType);
        System.out.println(hostName);
        System.out.println(userName);
        System.out.println(password);
        i++;
    }

}

props.properties:

 server1.serverName =1
 server1.serverType =2
 server1.hostName =3
 server1.userName =4
 server1.password =5

 server2.serverName =6
 server2.serverType =7
 server2.hostName =8
 server2.userName =9
 server2.password =10
smajlo
  • 972
  • 10
  • 21
0
Properties properties = new Properties();
properties.load(new FileInputStream("application.properties"));
int i = 1;
String serverNameKey = "server" + i + ".serverName";

while (properties.containsKey(serverNameKey)) {
    String serverName = (String) properties.get(serverNameKey);
            //Read other properties
    // Create new ServerDetails
    // Add to list
    i++;
}
Shamim Ahmmed
  • 8,265
  • 6
  • 25
  • 36