You can use Apache Commons Configuration 2:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.1.1</version>
</dependency>
Input file demo.properties
:
# This is the file header
#
# Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
# aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
# Database user
db.user=admin
# Database password
db.password=password
# Database URL
db.url=localhost
# This is the file footer
#
# Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
# occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Demo application reading, modifying and writing a new properties file:
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.PropertiesConfigurationLayout;
import org.apache.commons.configuration2.ex.ConfigurationException;
import java.io.*;
class PropertiesFileWithCommentsDemo {
public static void main(String[] args) throws IOException, ConfigurationException {
// Load existing properties file
File file = new File("demo.properties");
PropertiesConfiguration config = new PropertiesConfiguration();
PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout();
layout.load(config, new InputStreamReader(new FileInputStream(file)));
// Print some information from the properties file
System.out.println(layout.getHeaderComment());
System.out.println(layout.getKeys());
System.out.println(layout.getFooterComment());
// Change existing property
config.setProperty("db.password", "secret");
// Add new property + comment + leading blank line
config.setProperty("new.property", "Hello world!");
layout.setComment("new.property", "This is a newly added property");
layout.setBlancLinesBefore("new.property", 1);
// Save to new properties file
layout.save(config, new FileWriter("demo-append.properties", false));
}
}
Console log:
# This is the file header
#
# Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
# aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
[db.user, db.password, db.url]
# This is the file footer
#
# Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
# occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Output file demo-append.properties
:
# This is the file header
#
# Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
# aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
# Database user
db.user=admin
# Database password
db.password=secret
# Database URL
db.url=localhost
# This is a newly added property
new.property = Hello world!
# This is the file footer
#
# Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
# occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.