3

This is a follow-up to my previous question (found here). My properties files so far consists of simple key-value pairs, like ints and Strings. I would now like to use it to place some more advanced structures in it, more specifically I need a Map<A, Integer> where A is a class i have defined, for example like so:

foo=bar,5;baz,10

Is this possible? If so, how should I format the file and parse the map, respectively?

Are there perhaps better ways to solve this?

4 Answers 4

3

Have you considered using a split() and trim().

Please see below:

ResourceBundle rb = ResourceBundle.getBundle(Prop.class.getName());

    String fooValue = rb.getString("foo");
    String[] firstSplit = fooValue.split(";");
    for(String first : firstSplit){
        String firstTrim = first.trim();

        String[] intAndString = firstTrim.split(",");
        if(intAndString.length == 2){
            String intString = intAndString[0].trim();
            String stringVal = intAndString[1].trim();

            //TODO add entries here or return value.

        }
    }
2
  • What if your string needs to contain a comma or semi-colon?
    – Tom
    Sep 4, 2009 at 17:34
  • I am definitely sure this is not going to be an issue. Also, it's always possible to exchange it to other characters.
    – pg-robban
    Sep 4, 2009 at 17:37
1

It might be overkill for what you're trying to do and your long term plans but I've found that this kind of thing is very easily handled by the Spring Framework.

Here's a reference showing how to handle collections including maps.

Here's another general article on Spring

Here's what the map looks like in the Spring configuration file

<beans>
    <bean id="foo" class="x.y.Foo">
        <property name="accounts">
            <map>
                <entry key="one" value="9.99"/>
                <entry key="two" value="2.75"/>
                <entry key="six" value="3.99"/>
            </map>
        </property>
    </bean>
</beans>
1

Use XML not properties files with your own language to represent more complicated structures, it'll be less work, less buggy, easier to maintain.

You can use binding technologies such as JIBX and XML Beans to make things easier.

1

The easiest thing to do in this example would be to create a separate properties file for it. Then it just has name/value pairs, like

bar=5 baz=10

If this is a big table, I wouldn't want to mix it in with more general stuff. If it's a table with 3 pairs, that's different.

Properties files are really the best solution for complex data structures. They're meant for name/value pairs. If you get beyond that, sure, you can make it work by inventing a syntax, but why bother? If it's a super complex syntax, XML is a possibility. Or maybe it belongs in a relational database. Or a CSV file. Choose a tool that matches the requirement, don't decide on the tool first and then try to make it fit.

1
  • I actually ended up using a combination of separate property files along with Koekiebox's solution. I create instances of the class by parsing using the read string and map it. I am fairly happy with this solution as it works well so far, not to mention it's fairly readable.
    – pg-robban
    Sep 4, 2009 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.