[英]Writing a .properties file in Java destroys the structure of the file
I am able to write to a Java .properties
file successfully with this code:我可以使用以下代码成功写入 Java .properties
文件:
Properties prop = new Properties();
prop.load(input);
prop.setProperty("myProp", "myValue");
prop.store(output, "");
But then when I look into my .properties
file, all the structure is gone!但是当我查看我的.properties
文件时,所有的结构都消失了! The fields are written in random order, and all the comments (that took me a lot of effort to write) have been overwritten with nothing!字段是按随机顺序写的,所有的评论(花了我很多精力写的)都被覆盖了!
How can I write to a .properties
file while making sure that its structure is conserved?如何写入.properties
文件,同时确保其结构是保守的?
You can use Apache Commons Configuration 2 :您可以使用Apache Commons 配置 2 :
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.1.1</version>
</dependency>
Input file demo.properties
:输入文件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
:输出文件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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.