Here is a situation I have encountered. I know I can write a custom code to get the job done, but I was wondering if the java.util.Properties class (or something else) had this inbuilt and I was missing it in the documentation.

Situation: Need to load from a file, a set of values (mutliple values) for an associated single key.

To illustrate, here is an example:
propfile.properties
CODE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>
    <entry key="fooA">barA1</entry>
    <entry key="fooA">barA2</entry>
    
    <entry key="fooB">barB1</entry>
</properties>


The fooB=barB1 is a trivial case in implementing using the Properties class. But what about the multiple case - fooA=[barA1,barA2] ? How do I get them into a HashMap or something?

Here is a independent code snippet that shows my attempt. It shows the trivial case without any problem.
PropPlay.java
CODE
import java.util.Properties;
import java.io.FileInputStream;

public class PropPlay {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(" --------------------------------------- ");
        System.out.println(" - This is a Java Properties file test - ");
        System.out.println(" --------------------------------------- \n");
        
        try {
        System.out.println("Loading cfg/propfile.properties");
        FileInputStream fis = new FileInputStream("cfg/propfile.properties");
        System.out.println("...loaded.\n");
        
        System.out.println("Loading to internal Properties object");
        Properties prop = new Properties();
        prop.loadFromXML(fis);
        System.out.println("...loaded.\n");
        
        System.out.println("Here are the properties:");
        prop.list(System.out);
        
        System.out.println("\nGetting properties for fooA: ");
        System.out.println(prop.getProperty("fooA"));
        
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Bailing out!!");
        }

    }

}


Output:
CODE
---------------------------------------
- This is a Java Properties file test -
---------------------------------------

Loading cfg/propfile.properties
...loaded.

Loading to internal Properties object
...loaded.

Here are the properties:
-- listing properties --
fooA=barA2
fooB=barB1

Getting properties for fooA:
barA2


The one key - multiple values obviously does not work as intended. Any idea how I can get it into a HashMap or some object like that?

 

 

 


Comment/Reply (w/o sign-up)