Pulling values from a Java Properties file in order?

前端 未结 15 760
庸人自扰
庸人自扰 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:21

    Working example :

    Map properties = getOrderedProperties(new FileInputStream(new File("./a.properties")));
    properties.entrySet().forEach(System.out::println);
    

    Code for it

    public Map getOrderedProperties(InputStream in) throws IOException{
        Map mp = new LinkedHashMap<>();
        (new Properties(){
            public synchronized Object put(Object key, Object value) {
                return mp.put((String) key, (String) value);
            }
        }).load(in);
        return mp;
    }
    

提交回复
热议问题