对两个文件进行比较并显示异同之开源解决(显示增加删除)

由于公司项目需要对两个文件进行比较.(其实是数据库文本字段)

 

并且显示异同.

 

这里不是进行一个简单的比较.有一个文件就是

 

text1  1234

text2   012324

 

这两个文件并不是一个字段都不同.所以用一个一个 对比的办法是不可能的.因为text2 只是在 text1的基础上

 

在首字母上面增加了一个0 其他的没有改变...由于项目紧..没有自己实现..(自己实现也没有什么太好的思路)

 

这里就不讨论原理了..


http://www.incava.org/projects/java/java-diff/

 

文件下载

 

这个是开源的解决方案..在网络上面找到的.分享下.

 

实现很简单一共有两个类..两jar包都没有..直接把源文件放到工程下面进行测试..

 

demo也是很简单的.

 

Object[] a = new Object[] {
        "a",
        "b",
        "c",
        "d",
        "e"
    };

    Object[] b = new Object[] {
        "a",
        "x",
        "y",
        "b",
        "c",
        "j",
        "e",
    };

    Difference[] expected = new Difference[] {
        new Difference(1, -1,  1,  2),
        new Difference(3,  3,  5,  5),
    };

    Diff diff = new Diff(a, b);
    List diffOut = diff.diff();
 

要求输入的是一个Object[] 或是一个Collection的接口...

 

这个就不说你是怎么将你的文件转换成数组了.

 

String strs = "123456";
		char[] strChars = strs.toCharArray();
		Object[] objs = new Object[strChars.length];
		for( int i  = 0; i < strChars.length; i ++){
			objs[i] = strChars[i];
		}

 下面看这个代码:

 

public static void main(String[] args) {
		Object[] a = new Object[] { "a", "b", "c", "d", "e" };
		Object[] b = new Object[] { "a", "x", "y", "b", "c", "j", "e", };
		Diff diff = new Diff(a, b);
		List diffOut = diff.diff();
		for (int i = 0; i < diffOut.size(); i++) {
			Difference diffce = (Difference) diffOut.get(i);
			System.out.println(diffce);
		}

	}

 在main函数里面运行:

 

显示结果:

del: [1,-1] add: [1,2]
del: [3,3] add: [5,5]

 

其中difference是显示不同的类下面有4个属性:

 

/**
     * The point at which the deletion starts.
     */
    private int delStart = NONE;

    /**
     * The point at which the deletion ends.
     */
    private int delEnd = NONE;

    /**
     * The point at which the addition starts.
     */
    private int addStart = NONE;

    /**
     * The point at which the addition ends.
     */
    private int addEnd = NONE;

 当delEnd 或 addEnd 为 -1 的时候表示不进行操作.

 

 

//abcde
		//01234
		//a<add>xy</add>cde
		//0     12      345
		//a<add>xy</add>c<del>d</del>e
		//0     12      3            4
		//a<add>xy</add>bc<del>d</del><add>j</add>e
		//0     12      34                 5      6
 

图示:<add>表示增加<del>表示删除.

del: [1,-1]  没有删除 add: [1,2]向a [1]中添加[1][2]
del: [3,3]  删除a中[3]  add: [5,5] 向a中添加[5]

 

[start,end]表示操作的数组下标从0开始.起始结束.

 

注:进行了del操作.数组长度减少.添加数组长度增加.

 

其中你自己调用.delStart,delEnd , addStart,addEnd就可以对数组进行标示了....

 

如果代码中的<add>换成其他的<b>标签就可以在html中.显示文件的不同了.

 

public static void main(String[] args) {
		Object[] a = new Object[] { "a", "b", "c", "d", "e" };
		Object[] b = new Object[] { "a", "x", "y", "b", "c", "j", "e" };
		Diff diff = new Diff(a, b);
		List diffOut = diff.diff();
		for (int i = 0; i < diffOut.size(); i++) {
			Difference diffce = (Difference) diffOut.get(i);
			System.out.println(diffce);
		}
		// abcde
		// 01234
		// a<add>xy</add>cde
		// 0 12 345
		// a<add>xy</add>c<del>d</del>e
		// 0 12 3 4
		// a<add>xy</add>bc<del>d</del><add>j</add>e
		// 0 12 34 5 6
		ArrayList list = new ArrayList();
		for (int i = 0; i < a.length; i++) {
			list.add(a[i]);
		}
		for (int i = 0; i < diffOut.size(); i++) {
			Difference tempDiff = (Difference) diffOut.get(i);
			int delStart = tempDiff.getDeletedStart();
			int delEnd = tempDiff.getDeletedEnd();
			int addStart = tempDiff.getAddedStart();
			int addEnd = tempDiff.getAddedEnd();
			if (delEnd != -1) {
				// delEnd == -1 就是不进行刪除...
				// 删除开始.
				for (int j = delStart; j <= delEnd; j++) {
					// 删除多个.
					list.remove(j);
					if (delStart != 0) {
						String temp = list.get(delStart - 1).toString();
						list.remove(delStart - 1);
						list
								.add(delStart - 1, temp + "<del>" + b[j]
										+ "</del>");
					} else {
						String temp = list.get(0).toString();
						list.remove(0);
						list.add(0, temp + "<del>" + b[j] + "</del>");
					}
					pirntList(list);
				}
			}

			if (addEnd != -1) {
				// addEnd == -1 就是不进行添加...
				// 下面添加的情况..
				for (int j = addStart; j <= addEnd; j++) {
					// 添加多个.
					if (addStart > list.size()) {
						list.add("<add>" + b[j] + "</add>");
					} else {
						list.add(addStart, "<add>" + b[j] + "</add>");
					}
					pirntList(list);
				}
			}
		}
		pirntList(list);
		// return buffer.toString();

	}

	public static void pirntList(List list) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			buffer.append(""+list.get(i));
		}
		System.out.println(buffer.toString());
	}
 

通过ArrayList 实现对数据的整理.用remove 删除.用add(index,object)进行添加.

 

结果:

 

del: [1,-1] add: [1,2]
del: [3,3] add: [5,5]
a<add>x</add>bcde
a<add>y</add><add>x</add>bcde
a<add>y</add><add>x</add><del>b</del>cde
a<add>y</add><add>x</add><del>b</del>cd<add>j</add>e
a<add>y</add><add>x</add><del>b</del>cd<add>j</add>e

 

最后显示了a变成b的全部过程.

 

 

 

 

 

 

 

 

你可能感兴趣的:(对两个文件进行比较并显示异同之开源解决(显示增加删除))