How can I write Java properties in a defined order?

前端 未结 5 953
粉色の甜心
粉色の甜心 2020-12-05 06:47

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.

5条回答
  •  自闭症患者
    2020-12-05 07:38

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

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

    提交回复
    热议问题