Pulling values from a Java Properties file in order?

前端 未结 15 762
庸人自扰
庸人自扰 2020-12-02 18:32

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

15条回答
  •  无人及你
    2020-12-02 19:27

    Extend java.util.Properties, override both put() and keys():

    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.HashSet;
    import java.util.LinkedHashSet;
    import java.util.Properties;
    import java.util.HashMap;
    
    public class LinkedProperties extends Properties {
        private final HashSet keys = new LinkedHashSet();
    
        public LinkedProperties() {
        }
    
        public Iterable orderedKeys() {
            return Collections.list(keys());
        }
    
        public Enumeration keys() {
            return Collections.enumeration(keys);
        }
    
        public Object put(Object key, Object value) {
            keys.add(key);
            return super.put(key, value);
        }
    }
    
        

    提交回复
    热议问题