使用场景:
在比较不同环境跑的同一文件时,需要查看具体的不同点。如果通过传统的手动比较的话,数据量少的时候没有问题。但是数据量一旦达到上百条甚至上万条时。用手动比较显然不是个明智的选择。
本工具提供了一种快速的同时多文件比较文件差异的方法。支持输出以下内容
1.A文件中多的行(数量及keys)
2.B文件中多的行(数量及keys)
2.A和B文件交集部分的不同信息(数量及keys、行信息)
使用举例:
文件A:
a|11|22
b|110|220
文件B:
a|11|22
b|111|222
将第一列作为key进行A/B文件的比较
输出结果为:
b所在的行内容有差异
代码:
支持文件内容的解密后进行比较
import com.google.common.collect.Sets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* 功能描述:对比新旧环境生成的文件
* <p>
* <p>
* 文件A: a|b|c|d|e|f
* 文件B: a|b|c|d|e|e
* 找出文件中的不同行
*
* @author zhenbo.ma
* @date 22/08/16 上午 10:26
*/
public class DataFileCompareUtils {
public static void main(String[] args) throws IOException {
String oldCommonPath = "E:\\chromeDown\\文件对比\\20220815\\";
String newCommonPath = "E:\\chromeDown\\文件对比\\20220815 (1)\\filelist\\";
String fileName = "b.txt";
compareAndOutputConsole(fileName
, fileToMap(oldCommonPath + fileName, null, 2)
, fileToMap(newCommonPath + fileName, null, 2));
fileName = "c.txt";
compareAndOutputConsole(fileName
, fileToMap(oldCommonPath + fileName, null, 3)
, fileToMap(newCommonPath + fileName, null, 3)
);
// 1,2 表示第2列和第3列做为联合主键
fileName = "d.txt";
compareAndOutputConsole(fileName
, fileToMap(oldCommonPath + fileName, null, 1,2)
, fileToMap(newCommonPath + fileName, null, 1,2)
);
// 12,14 表示忽略 第13,15列的差异性
fileName = "e.txt";
compareAndOutputConsole(fileName
, fileToMap(oldCommonPath + fileName, null, 2)
, fileToMap(newCommonPath + fileName, null, 2),12,14);
}
/**
* @param fileName 文件名:用于打印区别日志
* @param oldMap 老文件的文件内容
* @param newMap 新文件的文件内容
* @param ignoreKeys 需要忽略的文件位置
*/
private static void compareAndOutputConsole(String fileName, Map<String, String> oldMap, Map<String, String> newMap, int... ignoreKeys) {
Set<String> oldKey = oldMap.keySet();
Set<String> newKey = newMap.keySet();
// 老文件中多的
Set<String> differenceA = Sets.difference(oldKey, newKey);
System.err.println(fileName + ":老文件中多的key:" + differenceA.size() + "个:");
// 新文件中多的
Set<String> differenceB = Sets.difference(newKey, oldKey);
System.err.println(fileName + ":新文件中多的key:" + differenceB.size() + " 个:");
// 都有的
Set<String> intersection = Sets.intersection(newKey, oldKey);
HashSet<String> diffs = new HashSet<>(intersection.size());
for (String interKey : intersection) {
String[] oldValue = oldMap.get(interKey).split("\\|");
String[] newValue = newMap.get(interKey).split("\\|");
// 不相等
oldValue = ArrayUtils.removeAll(oldValue, ignoreKeys);
newValue = ArrayUtils.removeAll(newValue, ignoreKeys);
String oldStr = Arrays.toString(oldValue);
String newStr = Arrays.toString(newValue);
if (!oldStr.equals(newStr)) {
diffs.add(interKey);
}
}
System.err.println(fileName + "交集中的总个数" + intersection.size() + ":交集中不相等的:" + diffs.size() + "个:");
if (diffs.size() > 0) {
String next = diffs.iterator().next();
String newV = newMap.get(next);
String oldV = oldMap.get(next);
System.err.println(fileName + "交集中的不同: \nnew:" + newV + "\nold:" + oldV);
}
}
/**
* @param srcPath 将文件转换为map形式
* 举例: 文件格式: a|b|c|d|e|f
* 转化为 {@b@:a|b|c|d|e|f}
* @param encKey 如果文件需要解密的话,传入key,不需要的话传null
* @param keys 行文件的主键key位置,传多个的话支持组合主键
* @return
* @throws IOException
*/
public static Map<String, String> fileToMap(String srcPath, String encKey, int... keys) throws IOException {
if (StringUtils.isNotEmpty(encKey)) {
DESUtil.decryptByFile(srcPath, srcPath + "_des.txt", encKey);
srcPath = srcPath + "_des.txt";
}
File file = new File(srcPath);
List<String> strings = FileUtils.readLines(file);
Map<String, String> map = new HashMap<>(strings.size());
for (String string : strings) {
if (StringUtils.isNotEmpty(string)) {
String[] split = string.split("\\|");
StringBuilder mapKey = new StringBuilder();
for (int key : keys) {
mapKey.append("@").append(split[key]);
}
mapKey.append("@");
map.put(mapKey.toString(), string);
}
}
return map;
}
}
本工具中所依赖的三方包
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.5-jre</version>
</dependency>