简体   繁体   English

我们能否使用键值对格式的FileWriter和BufferedWrirter组合将数据写入config.properties文件

[英]Can we write data to a config.properties file using FileWriter and BufferedWrirter combination in a key value pair format

Using FileWriter , can I write the key value pair username = "login_data" to a .properties file instead of a .txt file? 使用FileWriter可以将键值对username = "login_data"写入.properties文件而不是.txt文件吗?

String value1 = "This is the value from FILE WRITER"; 
System.out.println("Key1 == " + value1);

FileWriter fw = new FileWriter(dataFilePath, true);
BufferedWriter bw = new BufferedWriter(fw);

bw.write(value1);
bw.close();

To store the properties it is much better to create a Properties object, put the values and save it via store() method. 要存储属性,最好创建一个Properties对象,放置值并通过store()方法将其保存。

If you want to keep layout and comments too, it's worth looking at Apache Commons PropertiesConfigurationLayout : 如果您也想保留布局和注释,那么值得看看Apache Commons PropertiesConfigurationLayout

PropertiesConfiguration config = new PropertiesConfiguration();
PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout();

config.setProperty("key", "value");
layout.setComment("key", "description of key");

layout.setHeaderComment("file description");

layout.save(config, new FileWriter (file));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM