Parsing string as properties

You’re right that java.util.Properties doesn’t have a method to read from a String – but in fact it has more general methods that read from an InputStream or Reader.

So you can call load if you have some way of presenting your String as either of these, i.e. a source which effectively iterates over characters one by one. This feels like it ought to exist, and indeed it does – java.io.StringReader.

Putting it together, then, is quite straightforward:

public Properties parsePropertiesString(String s) {
    // grr at load() returning void rather than the Properties object
    // so this takes 3 lines instead of "return new Properties().load(...);"
    final Properties p = new Properties();
    p.load(new StringReader(s));
    return p;
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)