简体   繁体   English

如何在Java属性文件中使用逗号分隔键值对

[英]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? 或者有没有办法将每个Name和Email键值对与唯一标识符分组? Basically, I want to have multiple Name and Email entries in one property file and to be able to get each of the entry. 基本上,我想在一个属性文件中有多个Name和Email条目,并且能够获得每个条目。

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. 您应该使用CSV,JSON,YAML,XML(但可能XML对于此用例来说太复杂)或数据库。

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

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