目录
前言
一、实现的功能
二、工具源码
三、使用用例
总结
前言
在项目中有可能会需要对比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
三、使用用例
以下是代码使用示例:
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");
}
}
总结
以上就是源码及使用用例,有需要的小伙伴可以直接拿来用,有任何疑问欢迎评论指出。另外如需转载请备名出处,谢谢。



