简体   繁体   中英

How to separate key value pairs with comma in Java properties file

Currently, my property file output looks like this

Name=Frank 
Email=frank@mail.com

based on the following codes

File prop = new File(".properties");

if (!prop.exists()) {
    prop.createNewFile();
}        

try {
    FileReader reader = new FileReader(prop);
    Properties properties = new Properties();
    properties.load(reader);
    reader.close();

    properties.setProperty("Name", name);
    properties.setProperty("Email", email);

    FileWriter writer = new FileWriter(prop);
    properties.store(writer, "Settings");
    writer.close();

} catch (IOException ex) {
    Logger.getLogger(PropertiesTest.class.getName()).log(Level.SEVERE, null, ex);
}

I want it to be written in one line, with each key value pair separated by a comma. So it will look like this

Name=Frank, Email=frank@mail.com

How can I achieve this? Or is there a way to group each Name and Email key value pairs with a unique identifier? Basically, I want to have multiple Name and Email entries in one property file and to be able to get each of the entry.

The properties file is not designed to do this. You should instead use CSV, JSON, YAML, XML (but probably XML is too complicated for this use case) or a database.

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