按顺序从 Java 属性文件中提取值? [英] Pulling values from a Java Properties file in order?

查看:27
本文介绍了按顺序从 Java 属性文件中提取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个属性文件,其中值的顺序很重要.我希望能够遍历属性文件并根据原始文件的顺序输出值.

I have a properties file where the order of the values is important. I want to be able to iterate through the properties file and output the values based on the order of the original file.

但是,由于属性文件是由支持的,如果我错了,请纠正我,一个不维护插入顺序的 Map,迭代器以错误的顺序返回值.

However, since the Properties file is backed by, correct me if I'm wrong, a Map that does not maintain insertion order, the iterator returns the values in the wrong order.

这是我正在使用的代码

Enumeration names = propfile.propertyNames();
while (names.hasMoreElements()) {
    String name = (String) names.nextElement();
    //do stuff
}

除了编写我自己的自定义文件解析器之外,还有没有办法恢复属性?

Is there anyway to get the Properties back in order short of writting my own custom file parser?

推荐答案

如果您可以更改属性名称,您可以使用数字或其他可排序前缀作为前缀,然后对 Properties KeySet 进行排序.

If you can alter the property names your could prefix them with a numeral or other sortable prefix and then sort the Properties KeySet.

这篇关于按顺序从 Java 属性文件中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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