如何在 Java 属性文件中存储数组
我目前正在制作一个需要加载并转换为数组的 .properties 文件。
每个属性键都可能存在 0-25 个。
我尝试了一些实现,但我只是坚持干净地做这件事。
有人有什么想法吗?
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] )的数组的一部分
属性文件的格式可以更改,我愿意想法。
I'm currently making a .properties file that needs to be loaded and transformed into an array.
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我可以建议使用分隔符并使用
String.split(delimiter)
示例属性文件:
MON=0800#Something#Something1, Something2
希望帮助
I can suggest using delimiters and using the
String.split(delimiter)
Example properties file:
MON=0800#Something#Something1, Something2
Hope that helps
要么定义一个不会成为潜在值的分隔符,要么学习使用 XML。
如果您仍然坚持使用属性,请使用将返回所有键的列表的方法之一。您的密钥似乎由三个部分组成:组标识符(foo、bar)、索引(1、2)和元素名称(文件名、过期)。拿到所有的钥匙,将它们分解成各自的组成部分。为每种类型的标识符创建一个列表,在处理列表时使用标识符来确定要添加到哪个列表。按照您所说的方式创建配对元素,然后将其添加到列表中即可!如果索引顺序很重要,请将其作为字段添加到配对元素中,或者在处理之前对键进行排序。
Either define a delimiter that will not be a potential value or learn to use XML.
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.
没完全明白你的意图。
请检查 Apache Commons 配置库 http://commons.apache.org/configuration/
您可以针对一个键拥有多个值,如下所示在
键=值1,值2
您可以将其读入数组
configuration.getAsStringArray("key")
Didn't exactly get your intent.
Do check Apache Commons configuration library http://commons.apache.org/configuration/
You can have multiple values against a key as in
key=value1,value2
and you can read this into an array as
configuration.getAsStringArray("key")
使用YAML文件作为属性,这支持将属性作为数组。
快速浏览一下YAML:
Use YAML files for properties, this supports properties as an array.
Quick glance about YAML:
我有自定义加载。属性必须定义为:
自定义加载:
I have custom loading. Properties must be defined as:
Custom loading:
我强烈建议使用 Apache Commons (http://commons.apache.org/configuration/)。它能够使用 XML 文件作为配置文件。使用 XML 结构可以轻松地将数组表示为值列表而不是特殊编号的属性。
I highly recommend using Apache Commons (http://commons.apache.org/configuration/). It has the ability to use an XML file as a configuration file. Using an XML structure makes it easy to represent arrays as lists of values rather than specially numbered properties.
这是另一种通过自己实现该机制来实现的方法。
这里我们认为数组应该从 0 开始并且索引之间没有空洞
这是一种测试方法:
希望这有帮助
,欢迎所有评论。
here is another way to do by implementing yourself the mechanism.
here we consider that the array should start with 0 and would have no hole between indice
Here is a way to test :
hope this helps
and all remarks are welcome..
正如用户“Skip Head”已经指出的那样,csv 或任何表格文件格式将更适合您的情况。
如果它适合您,也许这个Table实现可能会你感兴趣。
As user 'Skip Head' already pointed out, csv or a any table file format would be a better fitt in your case.
If it is an option for you, maybe this Table implementation might interest you.
我建议使用属性文件,引用 CSV 文件。
然后将 CSV 文件解析为集合/数组等。
属性文件似乎不适合此类数据。
I'd suggest having the properties file, reference a CSV file.
Then parse the CSV file into a collection/array etc instead.
Properties file seems wrong fit for this kind of data.
实际上所有答案都是错误的
简单:
foo.[0]filename
Actually all answers are wrong
Easy:
foo.[0]filename