/** * Tests whether the contents of two files equals each other by performing * a byte-by-byte comparison. Each byte must match each other in both files. * @param file1 The file to compare * @param file2 The other file to compare * @return True if file contents are equal, otherwise false. * @throws IOException Thrown if there is an underlying IO error while * attempt to compare the bytes. */ public static boolean equals(File file1, File file2) throws IOException { // file lengths must match if (file1.length() != file2.length()) { return false; } InputStream is1 = null; InputStream is2 = null; try { is1 = new FileInputStream(file1); is2 = new FileInputStream(file2); return equals(is1, is2); } finally { // make sure input streams are closed if (is1 != null) { try { is1.close(); } catch (Exception e) { } } if (is2 != null) { try { is2.close(); } catch (Exception e) { } } } }
/** * Tests whether the contents of two files equals each other by performing * a byte-by-byte comparison. Each byte must match each other in both files. * @param file1 The file to compare * @param file2 The other file to compare * @return True if file contents are equal, otherwise false. * @throws IOException Thrown if there is an underlying IO error while * attempt to compare the bytes. */ public static boolean equals(File file1, File file2) throws IOException { // file lengths must match if (file1.length() != file2.length()) { return false; } InputStream is1 = null; InputStream is2 = null; try { is1 = new FileInputStream(file1); is2 = new FileInputStream(file2); return equals(is1, is2); } finally { // make sure input streams are closed if (is1 != null) { try { is1.close(); } catch (Exception e) { } } if (is2 != null) { try { is2.close(); } catch (Exception e) { } } } }