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;
}

I use this code to load properties from a single DB column

public Properties buildProperties(String propertiesFromString, String entrySeparator) throws IOException {
    Properties properties = new Properties();
    properties.load(new StringReader(propertiesFromString.replaceAll(entrySeparator, "\n")));
    return properties;
}

with a simple test

@Test
public void testProperties() throws Exception {
    Properties properties = buildProperties("A=1;B=2;Z=x",";");
    assertEquals("1", properties.getProperty("A"));        
    assertEquals("2", properties.getProperty("B"));        
    assertEquals("3", properties.getProperty("C","3"));        
    assertNull(properties.getProperty("Y"));
    assertEquals("x", properties.getProperty("Z"));  
}

These two utility methods could write to and read from String via java.util.Properties object:

/**
 * Converts a {@link Properties} object to {@link String} and you can
 * also provide a description for the output.
 *
 * @param props an input {@link Properties} to be converted to {@link String}
 * @param desc  an input description for the output
 * @return an output String that could easily parse back to {@link Properties} object
 * @throws IOException If writing encounter a problem or if an I/O error occurs.
 */
private static String convert2String(final Properties props, String desc) throws IOException {
    final StringWriter sw = new StringWriter();
    String propStr;
    try {
        props.store(sw, desc);
        propStr = sw.toString();
    } finally {
        if (sw != null) {
            sw.close();
        }
    }

    return propStr;
}


/**
 * Converts {@link String} to {@link Properties}
 * @param propsStr an {@link String} input that is saved via convert2String method
 * @return a {@link Properties} object
 * @throws IOException if an error occurred when reading from the {@link StringReader}
 */
private static Properties convert2Properties(final String propsStr) throws IOException {
    final Properties props = new Properties();
    final StringReader sr = new StringReader(propsStr);
    try {
        props.load(sr);
    } finally {
        if (sr != null)
            sr.close();
    }

    return props;
}

I found the above two methods useful when there are plenty of properties and you want to save all of them in a storage or database, and you don't want to create a huge key-value store table.

Tags:

Java