Java+html实现⽂本对⽐
⽬录
⼀、准备
1.Java使⽤的库
2.前端使⽤的库
⼆、后端实现
⽤java-diff-utils 封装了⼀个⼯具类 DiffHandleUtils.java
import UnifiedDiffUtils;
import Patch;
import File;
import IOException;
import Files;
import ArrayList;
import HashMap;
import List;
import Map;
import Collectors;
public class DiffHandleUtils {
/**
* 对⽐两⽂件的差异,返回原始⽂件+diff格式
*
* @param original 原⽂件内容
* @param revised  对⽐⽂件内容
*/
public static List<String>diffString(List<String> original, List<String> revised){
return diffString(original, revised,null,null);
}
/**
* 对⽐两⽂件的差异,返回原始⽂件+diff格式
*
* @param original        原⽂件内容
* @param revised          对⽐⽂件内容
* @param originalFileName 原始⽂件名
* @param revisedFileName  对⽐⽂件名
*/
public static List<String>diffString(List<String> original, List<String> revised, String originalFileName, String revisedFileName){        originalFileName = originalFileName ==null?"原始⽂件": originalFileName;
revisedFileName = revisedFileName ==null?"对⽐⽂件": revisedFileName;
//两⽂件的不同点
Patch<String> patch =DiffUtils.diff(original, revised);
//⽣成统⼀的差异格式
List<String> unifiedDiff = ateUnifiedDiff(originalFileName, revisedFileName, original, patch,0);
if(unifiedDiff.size()==0){
/
/如果两⽂件没差异则插⼊如下
unifiedDiff.add("--- "+ originalFileName);
unifiedDiff.add("+++ "+ revisedFileName);
unifiedDiff.add("@@ -0,0 +0,0 @@");
}else if(unifiedDiff.size()>=3&&!(2).contains("@@ -1,")){
//如果第⼀⾏没变化则插⼊@@ -0,0 +0,0 @@
unifiedDiff.add(2,"@@ -0,0 +0,0 @@");
}
//原始⽂件中每⾏前加空格
List<String> original1 = original.stream().map(v ->" "+ v).List());
List<String> original1 = original.stream().map(v ->" "+ v).List());
/
/差异格式插⼊到原始⽂件中
return insertOrig(original1, unifiedDiff);
}
/**
* 对⽐两⽂件的差异,返回原始⽂件+diff格式
*
* @param filePathOriginal 原⽂件路径
* @param filePathRevised  对⽐⽂件路径
*/
public static List<String>diffString(String filePathOriginal, String filePathRevised){
//原始⽂件
List<String> original =null;
//对⽐⽂件
List<String> revised =null;
File originalFile =new File(filePathOriginal);
File revisedFile =new File(filePathRevised);
try{
original = Path());
revised = Path());
}catch(IOException e){
e.printStackTrace();
}
return diffString(original, revised, Name(), Name());
}
//统⼀差异格式插⼊到原始⽂件
public static List<String>insertOrig(List<String> original, List<String> unifiedDiff){
List<String> result =new ArrayList<>();
//unifiedDiff中根据@@分割成不同⾏,然后加⼊到diffList中
List<List<String>> diffList =new ArrayList<>();
List<String> d =new ArrayList<>();
for(int i =0; i < unifiedDiff.size(); i++){
String u = (i);
if(u.startsWith("@@")&&!"@@ -0,0 +0,0 @@".equals(u)&&!u.contains("@@ -1,")){                List<String> twoList =new ArrayList<>();
twoList.addAll(d);
diffList.add(twoList);
d.clear();
d.add(u);
continue;
}
if(i == unifiedDiff.size()-1){
d.add(u);
List<String> twoList =new ArrayList<>();
twoList.addAll(d);
diffList.add(twoList);
d.clear();
break;
}
d.add(u);
}
//将diffList和原始⽂件original插⼊到result,返回result
for(int i =0; i < diffList.size(); i++){
List<String> diff = (i);
List<String> nexDiff = i == diffList.size()-1?null: (i +1);
//含有@@的⼀⾏
String simb = i ==0? (2): (0);
String nexSimb = nexDiff ==null?null: (0);
//插⼊到result
insert(result, diff);
//解析含有@@的⾏,得到原⽂件从第⼏⾏开始改变,改变了多少(即增加和减少的⾏)            Map<String, Integer> map =getRowMap(simb);
if(null!= nexSimb){
if(null!= nexSimb){
Map<String, Integer> nexMap =getRowMap(nexSimb);
int start =0;
("orgRow")!=0){
start = ("orgRow")+ ("orgDel")-1;
}
int end = ("revRow")-2;
//插⼊不变的
insert(result,getOrigList(original, start, end));
}
ains("@@ -1,")&&null== nexSimb){
insert(result,getOrigList(original,0, original.size()-1));
}else if(null== nexSimb && ("orgRow")< original.size()){
insert(result,getOrigList(original, ("orgRow"), original.size()-1));
}
}
return result;
}
//将源⽂件中没变的内容插⼊result
public static void insert(List<String> result, List<String> noChangeContent){
for(String ins : noChangeContent){
result.add(ins);
}
}
//解析含有@@的⾏得到修改的⾏号删除或新增了⼏⾏
public static Map<String, Integer>getRowMap(String str){
Map<String, Integer> map =new HashMap<>();
if(str.startsWith("@@")){
String[] sp = str.split(" ");
String org = sp[1];
String[] orgSp = org.split(",");
//源⽂件要删除⾏的⾏号
map.put("orgRow", Integer.valueOf(orgSp[0].substring(1)));
//源⽂件删除的⾏数
map.put("orgDel", Integer.valueOf(orgSp[1]));
String[] revSp = org.split(",");
//对⽐⽂件要增加⾏的⾏号
map.put("revRow", Integer.valueOf(revSp[0].substring(1)));
map.put("revAdd", Integer.valueOf(revSp[1]));
}
return map;
}
//从原⽂件中获取指定的部分⾏
public static List<String>getOrigList(List<String> original1,int start,int end){
List<String> list =new ArrayList<>();
if(start <= end && end < original1.size()){
for(; start <= end; start++){
list.(start));
}
}
return list;
}
}
测试:
public static void main(String[] args)throws IOException, PatchFailedException {        List<String> diffString = DiffHandleUtils.diffString("D:\\","D:\\");        diffString.forEach(System.out::println);
}
输出;
---
+++
@@ -1,1+1,3 @@
-1111
+
+aaaa
+bbbb
2222
@@ -3,1+5,1 @@
-3333
+3
4444
@@ -5,1+7,1 @@
-5555
+5555qqqq
6666
@@ -7,1+9,1 @@
-7777
+
8888
9999java修改html文件
@@ -10,0+12,2 @@
+xxxx
+cccc
三、前端实现
把上⾯获取到的 diffString 粘贴到下⾯html的 diffString 中
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8"/>
<!-- Make sure to load the highlight.js CSS file before the Diff2Html CSS file -->
<link rel="stylesheet"href="cdnjs.cloudflare/ajax/libs/highlight.js/10.7.1/styles/github.min.css"/>
<link
rel="stylesheet"
type="text/css"
href="cdn.jsdelivr/npm/diff2html/bundles/css/diff2html.min.css"
/>
<script type="text/javascript"src="cdn.jsdelivr/npm/diff2html/bundles/js/diff2html-ui.min.js"></script> </head>
<script>
const diffString =`
---
+++
@@ -1,1 +1,3 @@
-1111
+
+aaaa
+bbbb
2222
@@ -3,1 +5,1 @@
-3333
+3
4444
@@ -5,1 +7,1 @@
-5555
+5555qqqq
6666
@@ -7,1 +9,1 @@
-7777
+
8888
9999
@@ -10,0 +12,2 @@
+xxxx
+cccc
`;
document.addEventListener('DOMContentLoaded',function(){
var targetElement = ElementById('myDiffElement');
var configuration ={
drawFileList:true,
fileListToggle:false,
fileListStartVisible:false,
fileContentToggle:false,
matching:'lines',
outputFormat:'side-by-side',
synchronisedScroll:true,
highlight:true,
renderNothingWhenEmpty:true,
};
var diff2htmlUi =new Diff2HtmlUI(targetElement, diffString, configuration);
diff2htmlUi.draw();
diff2htmlUi.highlightCode();
});
</script>
<body>
<div id="myDiffElement"></div>
</body>
</html>
浏览器打开html效果如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。