码迷,mamicode.com
首页 > 编程语言 > 详细

有序的方式读写Properties文件 —— 对java.util.Properties的改写

时间:2015-03-11 13:11:12      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

目的

前几天,要做一个JSP读写Properties的模块,意在让用户能在web界面编辑系统配置。我很自然的用了java的Porperties工具类,读出写入不知多方便,但是用户说修改一个保存之后下次来就找不到自己的东西了(顺序乱了)。于是需要做一个有序的Properties文件读写类。果断上stackoverflow,找了两个例子(链接不记得了),两种方式,抄下来。

方式一,LinkedProperties

package com.example.common;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class LinkedProperties extends Properties {

    private static final long serialVersionUID = 1L;

    private Map<Object, Object> linkMap = new LinkedHashMap<Object,Object>();

    @Override
    public synchronized Enumeration keys() {
        List<Object> keys = new ArrayList<Object>();
        for (Map.Entry<Object, Object> entry : linkMap.entrySet()) {
            keys.add(entry.getKey());
        }
        return Collections.enumeration(keys);
    }

    @Override
    public synchronized Object put(Object key, Object value){
        super.put(key, value);
        return linkMap.put(key, value);
    }

    @Override
    public synchronized boolean contains(Object value){
        return linkMap.containsValue(value);
    }

    @Override
    public boolean containsValue(Object value){
        return linkMap.containsValue(value);
    }

    @Override
    public synchronized Enumeration<Object> elements(){
        throw new UnsupportedOperationException(
          "Enumerations are so old-school, don‘t use them, "
        + "use keySet() or entrySet() instead");
    }

    @Override
    public Set<Map.Entry<Object, Object>> entrySet(){
        return linkMap.entrySet();
    }

    @Override
    public synchronized void clear(){
        super.clear();
        linkMap.clear();
    }

    @Override
    public synchronized boolean containsKey(Object key){
        return linkMap.containsKey(key);
    }

    public void store(Writer writer, String comments)
            throws IOException
    {
        super.putAll(linkMap);
        super.store(writer, comments);
    }

}
方式二,SequencedProperties

package com.example.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@SuppressWarnings({"rawtypes", "unchecked"})
public class SequencedProperties extends Properties {


    private static final long serialVersionUID = 1L;

    private List keyList = new ArrayList();

    @Override
    public synchronized Enumeration keys() {
        return Collections.enumeration(keyList);
    }

    @Override
    public synchronized Object put(Object key, Object value) {
        if (!containsKey(key)) {
            keyList.add(key);
        }

        return super.put(key, value);
    }

    @Override
    public synchronized Object remove(Object key) {
        keyList.remove(key);

        return super.remove(key);
    }

    @Override
    public synchronized void putAll(Map values) {
        for (Object key : values.keySet()) {
            if (! containsKey(key)) {
                keyList.add(key);
            }
        }
        super.putAll(values);
    }

}

有序的方式读写Properties文件 —— 对java.util.Properties的改写

标签:

原文地址:http://my.oschina.net/atanl/blog/385190

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!