我不是在使用第三方库,而是使用标准的jdk库。
private static void verifyDirsAreEqual(Path one, Path other) throws IOException {
Files.walkFileTree(one, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs)
throws IOException {
FileVisitResult result = super.visitFile(file, attrs);
// get the relative file name from path "one"
Path relativize = one.relativize(file);
// construct the path for the counterpart file in "other"
Path fileInOther = other.resolve(relativize);
log.debug("=== comparing: {} to {}", file, fileInOther);
byte[] otherBytes = Files.readAllBytes(fileInOther);
byte[] theseBytes = Files.readAllBytes(file);
if (!Arrays.equals(otherBytes, theseBytes)) {
throw new AssertionFailedError(file + " is not equal to " + fileInOther);
}
return result;
}
});
}
注意:这只是比较两个文件夹下的实际文件。如果你有空文件夹等,你也想比较,你可能需要做一些额外的事情。