I have a Java Properties object that I load from an in-memory String, that was previously loaded into memory from the actual .properties file like this:
this.propertyFilesCache.put(file, FileUtils.fileToString(propFile));
The util fileToString actually reads in the text from the file and the rest of the code stores it in a HashMap called propertyFilesCache.  Later, I read the file text from the HashMap as a String and reload it into a Java Properties object like so:
String propFileStr = this.propertyFilesCache.get(fileName);
Properties tempProps = new Properties();
try {
    tempProps.load(new ByteArrayInputStream(propFileStr.getBytes()));
} catch (Exception e) {
    log.debug(e.getMessage());
}
tempProps.setProperty(prop, propVal);
At this point, I've replaced my property in my in-memory property file and I want to get the text from the Properties object as if I was reading a File object like I did up above.  Is there a simple way to do this or am I going to have to iterate over the properties and create the String manually?
The properties files are processed in the order in which they appear on the command line. Each properties file can refer to properties that have already been defined by a previously processed properties file, using ${varname} syntax.
Properties is a subclass of Hashtable. It is used to maintain a list of values in which the key is a string and the value is also a string i.e; it can be used to store and retrieve string type data from the properties file. Properties class can specify other properties list as it's the default.
This class is thread-safe: multiple threads can share a single Properties object without the need for external synchronization.
public static String getPropertyAsString(Properties prop) {    
  StringWriter writer = new StringWriter();
  prop.list(new PrintWriter(writer));
  return writer.getBuffer().toString();
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With