FileComparator implements Comparator<File>, FileFilter { public int compare(File f1, File f2) { InputStream in1 = null; InputStream in2 = null; try { boolean f1Exists = f1.exists(); boolean f2Exists = f2.exists(); if (f1Exists != f2Exists) { return f1Exists ? -1 : 1; } if (!f1Exists && !f2Exists) { return 0; } boolean isDir1 = f1.isDirectory(); boolean isDir2 = f2.isDirectory(); if (isDir1 != isDir2) { return isDir1 ? -1 : 1; } if (!isDir1) { in1 = new FileInputStream(f1); in2 = new FileInputStream(f2); return IOUtil.compare(in1, in2); } File[] files1 = f1.listFiles(filter); File[] files2 = f2.listFiles(filter); int ret = files1.length - files2.length; if (ret != 0) { return ret; } Arrays.sort(files1); Arrays.sort(files2); for (int i = 0; i < files1.length; i++) { File child1 = files1[i]; File child2 = files2[i]; ret = compare(child1, child2); if (ret != 0) { return ret; } } return 0; } catch (FileNotFoundException e) { throw new RuntimeException(e); } finally { IOUtil.close(in1); IOUtil.close(in2); } }  FileFilter getFilter(); void setFilter(FileFilter filter); int compare(File f1, File f2); boolean accept(File file);  }
@Test public void testCompareDirectory() { assertEquals(0, FileUtil.compare("src", "src")); assertTrue(FileUtil.compare("src", "target") != 0); assertTrue(FileUtil.compare("src", "src-notfound") < 0); assertTrue(FileUtil.compare("src-notfound", "src") > 0); assertTrue(FileUtil.compare("src-notfound", "src-notfound-2") == 0); }
@Test public void testCompareFile() { assertEquals(0, FileUtil.compare("pom.xml", "pom.xml")); assertTrue(FileUtil.compare("pom.xml", "src/test/resources/log4j.properties") != 0); assertTrue(FileUtil.compare("pom.xml", "pom-notfound.xml") < 0); assertTrue(FileUtil.compare("pom-notfound.xml", "pom.xml") > 0); assertTrue(FileUtil.compare("pom-notfound.xml", "pom-notfound-2.xml") == 0); }
@Test public void testCompareCrazy() { assertTrue(FileUtil.compare("src", "pom.xml") < 0); assertTrue(FileUtil.compare("pom.xml", "src") > 0); }