Java property object to string
•
Java
I have a Java property object that I load from a string in memory, which was previously from the actual The properties file is loaded into memory as follows:
this.propertyFilesCache.put(file,FileUtils.fileToString(propFile));
Util filetostring actually reads text from a file, and the rest of the code stores it in a HashMap called propertyfilescache After that, I read the text from the HashMap as a string and reload it into the Java properties object, as shown below:
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 have replaced my properties in my memory properties file. I want to get the text from the properties object, just as I am reading the file object mentioned above Is there a simple way to do this, or will I have to iterate over properties and create strings manually?
Solution
public static String getPropertyAsString(Properties prop) {
public static String getPropertyAsString(Properties prop) { StringWriter writer = new StringWriter(); prop.list(new PrintWriter(writer)); return writer.getBuffer().toString(); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码