package com.thingworx.sdk.simple; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class FileCompare { public static void main(String[] args) throws IOException { FileCompare c = new FileCompare(); File file1 = new File("I:/opt/thingworx/tw_staging/in/hello-edge.txt"); // the old log on thingworx server; here is a copy locally. File file2 = new File("I:/opt/thingworx/tw_staging/out/hello-thingworx.txt");// the new log files int lineNumber = c.compareFile(file1, file2); System.out.println(lineNumber); } public int compareFile(File file1, File file2) throws IOException { BufferedReader br1 = null; InputStream is1 = null; BufferedReader br2 = null; InputStream is2 = null; int lineNumber = 1; try { is1 = new FileInputStream(file1); br1 = new BufferedReader(new InputStreamReader(is1, "UTF-8")); is2 = new FileInputStream(file2); br2 = new BufferedReader(new InputStreamReader(is2, "UTF-8")); String lineStr1 = null; String lineStr2 = null; while (((lineStr1 = br1.readLine()) != null) && ((lineStr2 = br2.readLine()) != null)&&(lineStr1.equals(lineStr2))) { System.out.println(lineStr1); lineNumber ++; } if (br1 != null) { br1.close(); } if (is1 != null) { is1.close(); } if (br2 != null) { br2.close(); } if (is2 != null) { is2.close(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (br1 != null) { br1.close(); if (br2 != null) { br2.close(); } if (is1 != null) { is1.close(); }if (is2 != null) { is2.close(); } } } return lineNumber; } public static int copyFile(String src, String dst) { try { int len = 0; byte[] buf = new byte[1024]; FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dst); while ( (len = fis.read(buf)) != -1) { fos.write(buf, 0, len); } fis.close(); fos.close(); } catch (IOException e) { System.out.println(e); return -1; } return 0; } }