public class CompareDemo { // 可执行程序的路径 private String comparePath; // 执行脚本文件所在的路径 private String execJSPath; public CompareDemo(String comparePath, String execJSPath) { this.comparePath = comparePath; this.execJSPath = execJSPath; } // 比较两个文件 public boolean compareFile(String compareFile1, String compareFile2, String path) { String[] args = new String[] { comparePath, "@" + execJSPath, compareFile1.replace(" ", ""), compareFile2.replace(" ", ""), path }; run(args); return true; } /** * 执行一个命令行的方法 * * @param array * @return */ public final int run(String... array) { try { ProcessBuilder builder = new ProcessBuilder(); ProcessBuilder newBuilder = builder.command(array); builder.redirectErrorStream(true); // 创建进程, 执行发布任务 Process process = newBuilder.start(); InputStream inSTest = process.getInputStream(); InputStreamReader reader = new InputStreamReader(inSTest); BufferedReader bfReader = new BufferedReader(reader); String strLine = ""; while ((strLine = bfReader.readLine()) != null) { System.out.println(strLine); // log.info(strLine + "\r\n"); } bfReader.close(); process.waitFor(); process.destroy(); return process.exitValue(); } catch (Exception e) { e.printStackTrace(); } return -10000; } }
public class TestCompareDemo { @Test public void test() { String comparePath = "D:\\Program Files\\Beyond Compare 3\\BCompare.exe"; String execJSPath = "D:\\temp\\TestBYCompare\\compare.txt"; CompareDemo compareDemo = new CompareDemo(comparePath, execJSPath); compareDemo.compareFile("D:\\temp\\TestBYCompare\\old", "D:\\temp\\TestBYCompare\\new", "D:\\temp\\TestBYCompare\\test.html"); } }
file-report layout:side-by-side & options:ignore-unimportant,display-context,line-numbers & output-to:%3 output-options:html-color,wrap-word %1 %2