Property file same key with different value in java

I have a property file like this.

host=192.168.1.1
port=8060
host=192.168.1.2
port=8070
host=192.168.1.3
port=8080
host=192.168.1.4
port=8090

Now I want the unique url so I can pass it to other application. Example

HostOne   : https://192.168.1.1:8060
HostTwo   : https://192.168.1.2:8070
HostThree : https://192.168.1.3:8080
HostFour  : https://192.168.1.4:8090

How can I get it using Java or any other library. Please help.

Thanks.

EDITED

How about this if I will this type of data.

host=192.168.1.1,8060
host=192.168.1.1,8060
host=192.168.1.1,8060
host=192.168.1.1,8060

Now is there any way to get this. ?

Jon Skeet
people
quotationmark

Basically that property file is broken. A property file is a sequence of key/value pairs which is build into a map, so it requires the keys be unique. I suspect that if you load this into a Properties object at the moment, you'll get just the last host/port pair.

Options:

  • Make this a real properties file by giving unique keys, e.g.

    host.1=192.168.1.1
    port.1=8060
    host.2=192.168.1.2
    port.2=8070
    ...
    
  • Use a different file format (e.g. JSON)

  • Write your own custom parser which understands your current file format, but don't call it a "properties file" as that has a specific meaning to Java developers

Personally I'd probably go with JSON. For example, your file could be represented as:

[
    { "host": "192.168.1.1", "port": 8060 },
    { "host": "192.168.1.2", "port": 8070 },
    { "host": "192.168.1.3", "port": 8080 },
    { "host": "192.168.1.4", "port": 8090 }
]

people

See more on this question at Stackoverflow