Properties File multi-line values using PropertiesConfiguration
I created a work around in case anyone else needs this functionality. Also, there is probably a better way to do this, but this solution currently works for me.
First, set your PropertiesConfiguration delimiter to the new line character like so:
PropertiesConfiguration config = new PropertiesConfiguration(configFile);
config.setListDelimiter('\n');
Then you will need to iterate through and update all properties (to set the format):
Iterator<String> keys = config.getKeys();
while (keys.hasNext()) {
String key = keys.next();
config.setProperty(key,setPropertyFormatter(key, config.getProperty(key))) ;
}
use this method to format your value list data (as shown above):
private List<String> setPropertyFormatter(String key, Object list) {
List<String> tempProperties = new ArrayList<>();
Iterator<?> propertyIterator = PropertyConverter.toIterator(list, '\n');;
String indent = new String(new char[key.length() + 1]).replace('\0', ' ');
Boolean firstIteration = true;
while (propertyIterator.hasNext()) {
String value = propertyIterator.next().toString();
Boolean lastIteration = !propertyIterator.hasNext();
if(firstIteration && lastIteration) {
tempProperties.add(value);
continue;
}
if(firstIteration) {
tempProperties.add(value + ",\\");
firstIteration = false;
continue;
}
if (lastIteration) {
tempProperties.add(indent + value);
continue;
}
tempProperties.add(indent + value + ",\\");
}
return tempProperties;
}
Then it is going to be almost correct, except the save function takes the double backslash that is stored in the List, and turns it into 4 back slashes in the file! So you need to replace those with a single backslash. I did this like so:
try {
config.save(new File(filePath));
byte[] readIn = Files.readAllBytes(Paths.get(filePath));
String replacer = new String(readIn, StandardCharsets.UTF_8).replace("\\\\\\\\", "\\");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, false), "UTF-8"));
bw.write(replacer);
bw.close();
} catch (ConfigurationException | IOException e) {
e.printStackTrace();
}
apache-commons-config PropertiesConfiguration: comments after last property is lost
This is a bug that should be reported in the project's JIRA :)
https://issues.apache.org/jira/browse/CONFIGURATION
Can I put comments in between or after multiline properties in a Java Properties file?
Unfortunately, it's not possible as Java properties files can have only single line # comments
.
However, you might be aware that you can also define properties in xml format and the XML syntax will let you enter multi-line comments.
If you decide to give XML a chance then you will need to call Properties#loadFromXML(InputStream)
to load your XML property file.
Multiple values in java.util.Properties
Try:
foo=1,2
String[] foos = properties.getProperty("foo").split(",");
Write multi-line string in Spring boot .conf file
Spring boot launch script will use the shell to source the .conf
file, so you can put any shell script syntax to write the configuration. I would prefer to use vars to format them in your case such as the following:
MEM_OPTS='-Xms256m -Xmx512m'
DISPLAY_NAME='visualvm.display.name=ApplicationWs'
JMXREMOTE_PORT='com.sun.management.jmxremote.port=3333'
JMXREMOTE_SSL='com.sun.management.jmxremote.ssl=false'
JMXREMOTE_AUTH='com.sun.management.jmxremote.authenticate=false'
JAVA_OPTS="${MEM_OPTS} -D${DISPLAY_NAME} -D${JMXREMOTE_PORT} -D${JMXREMOTE_SSL} -D${JMXREMOTE_AUTH}"
see here
Related Topics
How to Skip @Postconstruct When Unit Testing
How to Sort Integer Digits in Ascending Order Without Strings or Arrays
In Firebase After Uploading Image How to Get Url
How to Get the Autoincremented Id When I Insert a Record in a Table Via Jdbctemplate
How to Use Jackson to Deserialise an Array of Objects
How to Increase the Java Heap Size Permanently
Spring Rest Post Json Requestbody Content Type Not Supported
How to Get Http Response Code Using Selenium Webdriver
How to Send List of Long to Rest Controller Spring Boot
Java 8 Streams - How to Merge Elements from the List With the Same Fields to One Element and Sum Up
Spring Boot: Cannot Access Rest Controller on Localhost (404)
How to Configure Hikaricp in My Spring Boot App in My Application.Properties Files
String Match of Only 3 Specific Words
Check If One List Contains Element from the Other
What Happens When Base and Derived Classes Each Have Variables With the Same Name