一 需求
实现比对两个文件,第四种方法,使用 diffutils 库。
二 实例
2.1 准备
- 创建 maven 项目
- 导入 diffutils 库到 pom.xml 文件中
<dependency>
<groupId>com.googlecode.java-diff-utils</groupId>
<artifactId>diffutils</artifactId>
<version>1.2.1</version>
</dependency>
- 准备 different.html 放在项目根路径下,文件内容参考: https://blog.csdn.net/fengyang182/article/details/109508007 中该文件
- 准备两个需要待比对的文件,我准备的是两个 xml 文件,文件内容参考 https://blog.csdn.net/fengyang182/article/details/109508007 中 xml 文件内容。准备完毕。
2.2 代码实例
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class testDiffUtils {
private static final String DELETION = "<span style=\"background-color: #FB504B\">${text}</span>";
private static final String INSERTION = "<span style=\"background-color: #45EA85\">${text}</span>";
private String left = "";
private String right = "";
@Test
public void testCompare() throws IOException {
String DELETION = "<span style=\"background-color: #FB504B\">${text}</span>"<