How can I write Java properties in a defined order?

前端 未结 5 960
粉色の甜心
粉色の甜心 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:51

    To use a TreeSet is dangerous! Because in the CASE_INSENSITIVE_ORDER the strings "mykey", "MyKey" and "MYKEY" will result in the same index! (so 2 keys will be omitted).

    I use List instead, to be sure to keep all keys.

     List list = new ArrayList<>( super.keySet());
     Comparator comparator = Comparator.comparing( Object::toString, String.CASE_INSENSITIVE_ORDER );
     Collections.sort( list, comparator );
     return Collections.enumeration( list );
    
        

    提交回复
    热议问题