Java программа для просмотра файлов Diff
Result.jsp:
<%@page import="org.apache.commons.lang3.StringUtils"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="util.Utility"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table cellpadding="3" cellspacing="3" border="3">
<tr>
<th align="center"><b>Current BootConf</b></th>
<th><b>New BootConf</b></th>
</tr>
<%
ArrayList<String> currentHacks = (ArrayList<String>) request.getAttribute("currentHacks");
ArrayList<String> newHacks = (ArrayList<String>) request.getAttribute("newHacks");
int size = currentHacks.size() > newHacks.size() ? currentHacks.size() : newHacks.size();
Iterator<String> current = currentHacks.iterator();
/* String cur[]=new String[currentHacks.size()];
String newBootconf[]=new String[newHacks.size()]; */
String cur[]=new String[size];
String newBootconf[]=new String[size];
int count=0;
while (current.hasNext()) {
cur[count]=(String)current.next();
count++;
}
count=0;
Iterator<String> new_hacks = newHacks.iterator();
while (new_hacks.hasNext()) {
newBootconf[count]=(String)new_hacks.next();
count++;
}
for(int i=0;i<size;i++){ %>
<tr>
<%
if(!Utility.replaceNull(cur[i]).equalsIgnoreCase(Utility.replaceNull(newBootconf[i]))){
%>
<td bgcolor="red"><%= Utility.replaceNull(cur[i])%></td>
<td bgcolor="red"><%= Utility.replaceNull(newBootconf[i])%></td>
<%}else{ %>
<td><%= Utility.replaceNull(cur[i])%></td>
<td><%= Utility.replaceNull(newBootconf[i])%></td>
<%}%>
</tr>
<%}
%>
</table>
</body>
</html>
ServletDiff.java
Метод DoGet:
ArrayList<String> currentHacks = readFile(currentFolderPath, hacks);
ArrayList<String> newHacks = readFile(newFolderPath, hacks);
request.setAttribute("currentHacks",currentHacks);
request.setAttribute("newHacks",newHacks);
RequestDispatcher requestDispatcher;
requestDispatcher = request.getRequestDispatcher("/result.jsp");
requestDispatcher.forward(request, response);
**Method to Read File**
public ArrayList<String> readFile(String folderPath, String fileName) {
File f =null;
FileInputStream fis = null;
String line = null;
ArrayList<String> result= new ArrayList<String>();
int count=0;
try {
f=new File(folderPath + "\\" + fileName);
fis = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while ((line = br.readLine()) != null) {
result.add(StringEscapeUtils.escapeXml(line));
count++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}`
Сервисный класс
public static String replaceNull(String input) {
return input == null ? "" : input;
}