How to personally format a properties file in java?
java, properties
Solution
I think I see your problem...when you have:
#Server 1 configuration
url=192.168.1.1
port=6546
#Server 2 configuration
url=192.168.2.1
port=6454
Only the last value for url, port, etc... will be read in the properties file, as each subsequent one will overwrite the previous. I typically got around this by doing something like:
#Server 1 configuration
server.1.url=192.168.1.1
server.1.port=6546
#Server 2 configuration
server.2.url=192.168.2.1
server.2.port=6454
....
#Server n configuration
server.n.url=192.168.2.1
server.n.port=6454
You can also store a total number of servers, with a property like:
number.of.servers=55
But that is assuming the server numbers are sequential. In the more general case lets assume that they are not so you can have something like:
#server 1
url=xxx
port=123
#server 100
url=xxx
port=123
#server 5
url=xxx
port=123
As @Nathan Hughes concisely pointed out properties are key value pairs, and each duplicate key will blow away the previous key. I have not used YAML, but I would much prefer JSON over XML. However you can still stick to properties.
As previously pointed out you could do something like this:
#server 1
server.1.url=xxx
server.1.port=123
#server 100
server.100.url=xxx
server.100.port=123
#server 5
server.5.url=xxx
server.5.port=123
So now the question becomes, which server numbers are in my properties file. Just iterate over the keys of the properties file, and do some String kung fu.
Properties p = myReadProperties();
Set<String> keys = p.keySet();
Set<Integer> serverNumbers = new Set<Integer>();
Iterator<String> i = keys.iterator();
String str, strArray;
while(i.hasNext()) {
str = i.next();
//A regex would be better here, but for brevity.
if(str.startsWith("server.") && str.endsWith(".url")) {
strArray = str.split(".");
serverNumbers.add(new Integer(strArray[1]));
}
So now you have a set of the server numbers. You can reconstruct your properties keys to get any value.
Problem
I'm using properties class from java to save some configuration for my application. It's the first time I'm using properties so please be gentle with me :) I can insert and retrieve data from the properties, no problem, but I want to insert the data as something like: Properties file: ``` #Header generated by java ~ this is fine, I don't care #Server 1 configuration url=192.168.1.1 port=6546 username=max password=123 #Server 2 configuration url=192.168.2.1 port=6454 username:dude password:123 #And so on... ``` This is my code: ``` public void setProp(String host, String port, String user, String pass, String host2, String port2, String user2, String pass2) { try{ prop.setProperty("host", host); prop.setProperty("port", porto); prop.setProperty("username", user); prop.setProperty("password", pass); prop.setProperty("host2", host2); prop.setProperty("port2", porto2); prop.setProperty("username2", user2); prop.setProperty("password2", pass2); config.store(new FileOutputStream("configuration.properties"), "Server 1 Configuration"); }catch (Exception e) { JOptionPane.showMessageDialog(null,"Error: "+e.getMessage()); } } ``` EDIT: @Nathan Not close to what I pretend. The properties file generated is: ``` #Wed Apr 03 14:03:57 BST 2013 server1.url=192.168.1.1 server1.port=80 server2.password=qqq server1.user=root server2.port=88 server2.user=dude server1.pass=123 server2.url=192.168.2.1 ``` I would be looking for something like: ``` #Wed Apr 03 14:03:57 BST 2013 #Server 1 details server1.url=192.168.1.1 server1.port=80 server1.user=root server1.pass=123 #Server 2 details: server2.password=qqq server2.port=88 server2.user=dude server2.url=192.168.2.1 ``` I dont even care if the order is not right (like password above url and under port, etc) I just need them to be grouped by, as they are in my example now.