<span style="font-family:Comic Sans MS;">package com.mstar.investmeplugin; import java.util.Collections; import java.util.Enumeration; import java.util.LinkedHashSet; import java.util.Properties; import java.util.Set; /** * OrderedProperties * @author liii * @date 2015-07-10 */ public class OrderedProperties extends Properties { private static final long serialVersionUID = -4627607243846121965L; private final LinkedHashSet<Object> keys = new LinkedHashSet<Object>(); public Enumeration<Object> keys() { return Collections.<Object> enumeration(keys); } public Object put(Object key, Object value) { keys.add(key); return super.put(key, value); } public Set<Object> keySet() { return keys; } public Set<String> stringPropertyNames() { Set<String> set = new LinkedHashSet<String>(); for (Object key : this.keys) { set.add((String) key); } return set; } }</span>
<span style="font-family:Comic Sans MS;">Properties props = new OrderedProperties(); props.load(fileInputStream); //通过 keys(), keySet() 或 stringPropertyNames 来遍历都能保证按文件中的顺序输出</span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
读配置文件可以保持顺序的 Java Properties 类
原文地址:http://blog.csdn.net/happylee6688/article/details/46894131