# Diff **Repository Path**: pior/Diff ## Basic Information - **Project Name**: Diff - **Description**: 这是一个用来比较两个文本内容的工具,可以得到两个文本的相同和不同处,还可以得到文本之间的变更补丁。 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 2 - **Created**: 2015-10-29 - **Last Updated**: 2025-03-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #Diff 其实这是一个源自网络的代码,传闻是一个google的工程自己写的一个小工具,似乎这个工具有python版本的,也不知道哪个是第一版。 先贴一段示例代码吧。 // 从文件读取文件内容 String file1 = readFile("d:/1.txt"); String file2 = readFile("d:/2.txt"); // 比较器对象 DiffMatchPatch diffMatchPatch = new DiffMatchPatch(); // 生成由file1升级为file2的补丁, 可以直接把这个字符串传递给其他人 String patchText = diffMatchPatch.getPatchString(file1, file2); System.out.println(patchText); // 在文件上打补丁, 获取新的文本内容 String newFile = diffMatchPatch.applyPatch(file1, patchText); System.out.println(newFile); // 比较升级文件和新文件的内容 Assert.assertEquals(file2, newFile);