我正在使用 java.util.Properties 的 store(Writer, String) 方法来存储属性。在生成的文本文件中,属性以随意的顺序存储。
这就是我正在做的:
Properties properties = createProperties();
properties.store(new FileWriter(file), null);
如何确保属性按字母顺序或添加属性的顺序写出?
我希望有一个比“手动创建属性文件”更简单的解决方案。
根据“The New Idiot”的建议,它按字母键顺序存储。
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)