Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing string as properties

Tags:

java

I am reading a properties file from database. I checked java.util.Properties and there's no method to parse from a String instance. Is there any way to do it?

like image 480
lucky_start_izumi Avatar asked Mar 12 '12 18:03

lucky_start_izumi


People also ask

What is string parsing?

String Parsing "Parsing" is the process of taking a string from a file or the network or the user, and processing it to extract the information we want. A common strategy is to use a few calls to indexOf() to figure out the index numbers of something interesting inside the string.


2 Answers

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; } 
like image 102
Andrzej Doyle Avatar answered Sep 23 '22 03:09

Andrzej Doyle


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.

like image 38
Mehdi Avatar answered Sep 25 '22 03:09

Mehdi