属性文件比较

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        String oldFileName = "D:\\test\\old.properties";
        String newFileName = "D:\\test\\new.properties";
        List<String> lines1 = getLines(oldFileName);
        List<String> lines2 = getLines(newFileName);

        List<Pro> pList1 = getPro(lines1);
        List<Pro> pList2 = getPro(lines2);

        List<Pro> p2NotContain = getTargetNotContain(pList1, pList2);
        List<Pro> p1NotContain = getTargetNotContain(pList2, pList1);

        System.out.println("需要增加的配置:");
        System.out.println(toProListString(p1NotContain));

        System.out.println("需要删除的配置:");
        System.out.println(toProListString(p2NotContain));

    }

    private static List<Pro> getTargetNotContain(List<Pro> source, List<Pro> target) {
        List<Pro> ret = new ArrayList<Pro>();
        for(Pro p1 : source){
            boolean isFoundInP2 = false;
            for(Pro p2 : target){
                if(p1.getKey().equals(p2.getKey())){
                    isFoundInP2 = true;
                    break;
                }
            }
            if(!isFoundInP2){
                ret.add(p1);
            }
        }
        return ret;
    }

    private static String toProListString(List<Pro> list){
        String ret = "";
        for(Pro item : list){
            ret += toProString(item);
            ret += "\n";
        }
        return ret;
    }

    private static String toProString(Pro pro){
        String ret = "";
        if(pro.getNote() != null && !"".equals(pro.getNote())){
            ret+= pro.getNote() + "\n";
        }
        ret += (pro.getKey() + "=" + pro.getValue());
        return ret;
    }

    private static List<Pro> getPro(List<String> lines) {
        List<Pro> ret = new ArrayList<Pro>();
        String note = null;
        String key = null;
        String value = null;
        for(String line : lines){
            if(line == null || "".equals(line)){
                continue;
            }
            if(line.startsWith("#")){
                note = line;
            }else{
                int index = line.indexOf("=");
                key = line.substring(0, index);
                value = line.substring(index + 1, line.length());
                ret.add(new Pro(note, key, value));
                note = null;
                key = null;
                value = null;
            }
        }
        return ret;
    }

    public static List<String> getLines(String fileName){
        List<String> lines=new ArrayList<String>();
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));
            String line= null;
            while ((line= br.readLine()) != null) {
                lines.add(line);
            }
            br.close();
        } catch (FileNotFoundException e){
        }catch (IOException e){}
        return lines;
    }

    private static class Pro{
        private String note;
        private String key;
        private String value;

        public Pro(String note, String key, String value) {
            this.note = note;
            this.key = key;
            this.value = value;
        }

        public String getNote() {
            return note;
        }

        public void setNote(String note) {
            this.note = note;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值