de.poiu.apron/apron
de.poiu.apron
hupfdule/apron
de.poiu.apron
gives you the ability to read and write properties files while preserving comments, whitespace, and order of entries.
java.util.Properties is one of the simpler ways to add configuration to a project. Properties files are just key value pairs separated by an equals sign.
key=value
other=otherValue
But if you want to edit a properties file programmatically while keeping any formatting, ordering, or commenting that a human did manually you will run into trouble.
This is the niche that de.poiu.apron
fills. You can have configuration files which are updated by a program and a human interchangeably.
import de.poiu.apron.PropertyFile;
import de.poiu.apron.entry.PropertyEntry;
import java.nio.file.Files;
import java.nio.file.Path;
void main() throws Exception {
= Path.of("config.properties");
var path = """
var fileContents =value
keyContext here
# =otherValue
otherKey""";
.writeString(path, fileContents);
Files
= PropertyFile.from(path.toFile());
PropertyFile file
// value
System.out.println(file.get("key"));
.appendEntry(new PropertyEntry("port", "4031"));
file
.saveTo(path.toFile());
file
// key=value
// # Context here
// otherKey=otherValue
// port = 4031
System.out.println(Files.readString(path));
}