栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用java 对比properties格式配置文件属性值

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用java 对比properties格式配置文件属性值

目录

前言

一、实现的功能

二、工具源码

三、使用用例

总结


前言

在项目中有可能会需要对比properties配置文件中的属性,如果是同一个配置文件可以通过git版本记录来进行对比,如果是不同的properties配置文件,且配置文件较多的话,手动对比相对来说费时费力,于是我手动实现一个文件对比的小功能。有需要的小伙伴尽管拿去。


一、实现的功能
1.获取两个文件中都存在且不相等的key-value
2.获取两个文件中都存在,且相等的key-value
3.获取第一个文件存在,第二个文件不存在的key-value
4.获取第二个文件存在,且第一个文件中不能存在的key-value

二、工具源码

以下是源码部分:

目录

前言

一、实现的功能

二、工具源码

三、使用用例

总结


package com.xm.file;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.function.BiFunction;


public class CompareProperties {

    private Properties firstProperties;

    private Properties secondProperties;

    
    public CompareProperties(String firstPropertiesPath, String secondPropertiesPath) throws Exception {
        checkPropertiesSource(firstPropertiesPath);
        checkPropertiesSource(secondPropertiesPath);
        this.firstProperties = loadProperties(firstPropertiesPath);
        this.secondProperties = loadProperties(secondPropertiesPath);
    }

    
    public List differentValue(){

        return compareNOtNullValue(String::equals);
    }


    
    public List sameValue(){
        return  compareNOtNullValue((s1,s2)->!s1.equals(s2));
    }


    
    public List firstNullValue(){

        return compareAndListNullValue(firstProperties,secondProperties);
    }


    
    public List secondNullValue(){

        return compareAndListNullValue(secondProperties,firstProperties);
    }


    private List compareNOtNullValue(BiFunction function){
        if (firstProperties.isEmpty() || secondProperties.isEmpty()) return new ArrayList<>(1);
        Iterator> iterator = firstProperties.entrySet().iterator();

        ArrayList list = new ArrayList<>();
        while (iterator.hasNext()){
            Map.Entry next = iterator.next();
            String firstValue = (String) next.getValue();
            if (stringIsEmpty(firstValue)) continue;
            String key = (String) next.getKey();
            if (!secondProperties.containsKey(key)) continue;
            String secondValue = secondProperties.getProperty(key);
            if (function.apply(firstValue,secondValue)) continue;
            list.add(new DoublePropertiesEntry(key,firstValue,secondValue));
        }
        return list;
    }

    private List compareAndListNullValue(Properties firstProperties,Properties secondProperties){
        if (firstProperties.isEmpty() || secondProperties.isEmpty()) return new ArrayList<>(1);
        Iterator> iterator = secondProperties.entrySet().iterator();

        ArrayList list = new ArrayList<>();
        while (iterator.hasNext()){
            Map.Entry next = iterator.next();
            String secondValue = (String) next.getValue();
            if (stringIsEmpty(secondValue)) continue;
            String key = (String) next.getKey();
            String firstValue = firstProperties.getProperty(key);
            if (stringIsEmpty(firstValue)){
                list.add(new DoublePropertiesEntry(key,firstValue,secondValue));
            }
        }
        return list;
    }

    private void  checkPropertiesSource(String path) throws Exception {
        if (null == path || "".equals(path)) throw new Exception("文件路径为空!");
        File file = new File(path);
        if(!file.exists()) throw new Exception("文件不存在!");
    }

    private Properties loadProperties(String path) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileReader(new File(path)));
        return properties;
    }

    private boolean stringIsEmpty(String string){
        return null == string || "".equals(string);
    }

   public class DoublePropertiesEntry {
       private String key;
       private Object firstValue;

       private Object secondVale;

        public DoublePropertiesEntry(String key, Object firstValue, Object secondVale) {
            this.key = key;
            this.firstValue = firstValue;
            this.secondVale = secondVale;
        }

        public String getKey() {
            return key;
        }

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

        public Object getFirstValue() {
            return firstValue;
        }

        public void setFirstValue(Object firstValue) {
            this.firstValue = firstValue;
        }

        public Object getSecondVale() {
            return secondVale;
        }

        public void setSecondVale(Object secondVale) {
            this.secondVale = secondVale;
        }
    }
}

三、使用用例

以下是代码使用示例:

package com.xm;

import com.xm.file.CompareProperties;


public class ComparePropertiesTest {
    public static void main(String[] args) throws Exception {

        String a = "C:\Users\xm\Desktop\a.properties";
        String b = "C:\Users\xm\Desktop\b.properties";

        CompareProperties compareProperties = new CompareProperties(a, b);

        System.out.println("nn=====================不同的键值对=============================nn");
        for (CompareProperties.DoublePropertiesEntry entry : compareProperties.differentValue()) {
            System.out.println("key: "+entry.getKey() +
                   " 第一个值: "+ entry.getFirstValue()+" 第二个值: "+entry.getSecondVale());
        }
        System.out.println("nn=======================相同的键值对===========================nn");

        for (CompareProperties.DoublePropertiesEntry entry : compareProperties.sameValue()) {
            System.out.println("key: "+entry.getKey() +
                    " 第一个值: "+ entry.getFirstValue()+" 第二个值: "+entry.getSecondVale());
        }
        System.out.println("nn======================第一个为空的键值对============================nn");

        for (CompareProperties.DoublePropertiesEntry entry : compareProperties.firstNullValue()) {
            System.out.println("key: "+entry.getKey() +
                    " 第一个值: "+ entry.getFirstValue()+" 第二个值: "+entry.getSecondVale());
        }
        System.out.println("nn=======================第二个为空的键值对===========================nn");
        for (CompareProperties.DoublePropertiesEntry entry : compareProperties.secondNullValue()) {
            System.out.println("key: "+entry.getKey() +
                    " 第一个值: "+ entry.getFirstValue()+" 第二个值: "+entry.getSecondVale());
        }

        System.out.println("nn==================================================nn");

    }
}


总结

以上就是源码及使用用例,有需要的小伙伴可以直接拿来用,有任何疑问欢迎评论指出。另外如需转载请备名出处,谢谢。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/996179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号