Returns true if the files located by the given paths exist, are not directories, and contain the same bytes. @throws IOException if an I/O error occurs @since 22.0
(Path path1, Path path2)
| 361 | * @since 22.0 |
| 362 | */ |
| 363 | public static boolean equal(Path path1, Path path2) throws IOException { |
| 364 | checkNotNull(path1); |
| 365 | checkNotNull(path2); |
| 366 | if (Files.isSameFile(path1, path2)) { |
| 367 | return true; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Some operating systems may return zero as the length for files denoting system-dependent |
| 372 | * entities such as devices or pipes, in which case we must fall back on comparing the bytes |
| 373 | * directly. |
| 374 | */ |
| 375 | ByteSource source1 = asByteSource(path1); |
| 376 | ByteSource source2 = asByteSource(path2); |
| 377 | long len1 = source1.sizeIfKnown().or(0L); |
| 378 | long len2 = source2.sizeIfKnown().or(0L); |
| 379 | if (len1 != 0 && len2 != 0 && len1 != len2) { |
| 380 | return false; |
| 381 | } |
| 382 | return source1.contentEquals(source2); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Like the unix command of the same name, creates an empty file or updates the last modified |