问题描述
我可以使用以下代码成功写入Java .properties
文件:
Properties prop = new Properties();
prop.load(input);
prop.setProperty("myProp","myValue");
prop.store(output,"");
但是当我查看.properties
文件时,所有结构都消失了!字段以随机顺序编写,所有注释(花了我很多精力编写)都被覆盖了!
如何在确保结构不变的情况下写入.properties
文件?
解决方法
您可以使用Apache Commons Configuration 2:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.1.1</version>
</dependency>
输入文件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.
演示应用程序读取,修改和写入新的属性文件:
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));
}
}
控制台日志:
# This is the file header
#
# Lorem ipsum dolor sit amet,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.
输出文件demo-append.properties
:
# This is the file header
#
# Lorem ipsum dolor sit amet,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.