java 文件行内容比较器

java行文件比较器

使用场景:

在比较不同环境跑的同一文件时,需要查看具体的不同点。如果通过传统的手动比较的话,数据量少的时候没有问题。但是数据量一旦达到上百条甚至上万条时。用手动比较显然不是个明智的选择。
本工具提供了一种快速的同时多文件比较文件差异的方法。支持输出以下内容
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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

b0b0大魔王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值