I have a properties file which currently contains system names and ip addresses. What would be the best way for me to store usernames with it as well. Can you use more then one element or key in a properties file? Thanks for any help and suggestions.
Create a class that has the fields you require along with the relevant getters and setters. Then you can set the fields to hold whatever you need and add these objects to your map as the value, e.g.:
public class SystemInfo {
private String systemName;
private String ipAddress;
private String username;
public SystemInfo() {
// do whatever
}
public void setSystemName(String name) {
this.systemName = name;
}
// etc.
}
Then you can create an instance of this, set the information required and store it in your map using whichever field you want as the key (or use some other data structure to store them), e.g.
SystemInfo system1 = new SystemInfo();
system1.setSystemName("The Name");
// etc.
Map<String, SystemInfo> systemMap = new HashMap<String, SystemInfo>();
systemMap.put(system1.getSystemName(), system1);
Are the usernames associated with particular systems? If so you could just create two properties for each system. One could have a key "(system)_address" and the other "(system)_username" This is assuming there's only one username per system
1.
system1.ip=10.100.151.1
system1.username=John
2.
Use database instead of properties file.
A Java .properties file is just a text file with key/value pairs in the form "key=value" or "key:value" and represented in Java as a Properties object which is a subclass of Hashtable. While you can store/load the file to XML, it is often much easier to use the default "key=value" form. The keys should be unique, and the values should not contain the delimiter ('=' or ':') unless escaped with a '\'. That said you can add whatever information to the file you want. For more detailed information, check out the API doc: http://java.sun.com/javase/6/docs/api/java/util/Properties.html#load%28java.io.Reader%29. You can use as many keys as desired, as long as they are unique. Depending on the problem you are trying to solve, there may be a better approach.