简体   繁体   中英

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?

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.

If you want to keep layout and comments too, it's worth looking at 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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