如何以定义的顺序编写Java属性? [英] How can I write Java properties in a defined order?

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

问题描述

我正在使用java.util.Properties的store(Writer,String)方法来存储属性。在生成的文本文件中,属性以偶然的顺序存储。

I'm using java.util.Properties's store(Writer, String) method to store the properties. In the resulting text file, the properties are stored in a haphazard order.

这就是我正在做的事情:

This is what I'm doing:

Properties properties = createProperties();
properties.store(new FileWriter(file), null);

如何确保按字母顺序或按添加属性的顺序写出属性?

How can I ensure the properties are written out in alphabetical order, or in the order the properties were added?

我希望解决方案比手动创建属性文件更简单。

I'm hoping for a solution simpler than "manually create the properties file".

推荐答案

根据新白痴的建议,按字母顺序排列。

As per "The New Idiot's" suggestion, this stores in alphabetical key order.

Properties tmp = new Properties() {
    @Override
    public synchronized Enumeration<Object> keys() {
        return Collections.enumeration(new TreeSet<Object>(super.keySet()));
    }
};
tmp.putAll(properties);
tmp.store(new FileWriter(file), null);

这篇关于如何以定义的顺序编写Java属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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