在java属性文件中表示数组的更好方法 [英] Better way to represent array in java properties file

查看:35
本文介绍了在java属性文件中表示数组的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一个需要加载并转换为数组的 .properties 文件.但是每个属性键都有可能存在 0-25 个.我尝试了一些实现,但我只是坚持干净利落地做到这一点.有人有什么想法吗?

I'm currently making a .properties file that needs to be loaded and transformed into an array. But there is a possibility of anywhere from 0-25 of each of the property keys to exist. I tried a few implementations but i'm just stuck at doing this cleanly. Anyone have any ideas?

foo.1.filename=foo.txt
foo.1.expire=200

foo.2.filename=foo2.txt
foo.2.expire=10

etc more foo's

bar.1.filename=bar.txt
bar.1.expire=100

我会将文件名/过期配对组合成一个数据对象,作为每个父属性元素的数组的一部分,例如 foo[myobject]

where I'll assemble the filename/expire pairings into a data object, as part of an array for each parent property element like foo[myobject]

属性文件的格式可以改变,我愿意接受想法.

Formatting of the properties file can change, I'm open to ideas.

推荐答案

要么定义一个没有潜在价值的分隔符,要么学习使用 XML.

Either define a delimiter that will not be a potential value or learn to use XML.

如果您仍然坚持使用属性,请使用将返回所有键列表的方法之一.您的密钥似乎由三部分组成:组标识符 (foo, bar)、索引 (1, 2) 和元素名称 (filename, expire).得到所有的钥匙,把它们分解成它们的组成部分.为每种类型的标识符创建一个列表,在处理列表时使用标识符来确定要添加到哪个列表.按照您的说法创建配对元素,然后简单地添加到列表中!如果索引顺序很重要,请将其作为字段添加到配对元素中或在处理之前对键进行排序.

If you still insist on using properties use one of the methods that will return a list of all keys. Your key appears to have three parts a group identifier (foo, bar) an index (1, 2) and then an element name (filename, expire). Get all the keys break them into their component parts. Create a List for each type of identifier, when processing the list use the identifier to determine which List to add to. Create you paired elements as you said and simply add to the list! If the index order is important either add that as a field to your paired elements or sort the keys before processing.

这篇关于在java属性文件中表示数组的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆