在java中读取带有数组的配置文件
我制作了一个java应用程序,它可以操作一些作为输入的矩阵,然后输出一些文本。目前,矩阵在我的应用程序中被硬编码(用于测试),但对于新矩阵来说重新编译是相当愚蠢的。
所以我正在寻找一种适合输入矩阵的配置文件格式。
我知道我可以轻松读取ini文件( 在 Java 中解析 INI 文件的最简单方法是什么?),但是将 [ [ 1, 2, 3], [4, 5, 6] ] 之类的东西解析为矩阵可能需要烦人的工作细绳操纵。
有谁知道读取这些矩阵的好方法?
I've made a java application that manipulates a few matrices it gets as input, and then outputs some text. Currently the matrices are hardcoded in my application (for testing), but recompiling is quite silly for a new matrix.
So I'm looking for a config file format that would be suited for inputting matrices.
I know I can read ini files easily ( What is the easiest way to parse an INI file in Java?), but then parsing a thing like [ [ 1, 2, 3], [4, 5, 6] ] to a matrix would probably require annoying string manipulation.
Does anyone know of a nice method to read in these matrices?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 JSON 作为数据交换格式。查看 Java 中的 JSON。
You can use JSON as the data interchange format. Take a look at JSON in Java.
CSV 文件怎么样?
你的输入可能看起来像
There are also some alreadybuilt CSV parser around... like http://commons .apache.org/sandbox/csv/
how about CSV files?
your input could look something like
There are also some already built CSV parser around... like http://commons.apache.org/sandbox/csv/
我其次是 csv 文件。然而,使用属性文件也很方便,一行代表一个矩阵。像这样的事情:
然后使用 java.util.Properties 读取文件。
解析:
这里的技巧是选择分隔符,它们不能出现在条目本身中。
I second on csv files. However, it can also be handy to use property files, one line representing one matrix. Something like this:
And then using java.util.Properties to read the file.
Parsing:
The trick here is in choosing separators, they must not be present in the entries themselves.