JDK 12的Files.mismatch方法
JDK 12向Files類引入了一種新方法。 方法Files.mismatch(Path,Path)已通過JDK-8202302引入JDK 12,并在JDK 12 Early Access Build 20 (支持新{@systemProperty} Javadoc標記的相同早期訪問版本)中可用 。
JDK-8202302 [“用于比較文件的(fs)New Files.mismatch方法”]添加Files.mismatch(Path,Path)方法“比較兩個文件的內(nèi)容以確定它們之間是否存在不匹配”,并且可以用于確定“兩個文件是否相等”。 曾有一次添加File.isSameContent()方法的討論 ,但由于它與“ Arrays.mismatch和Buffer.mismatch方法”的一致性,因此決定使用Files.mismatch(Path,Parh) 。
下一個代碼清單包含一個簡單的Java類,該類演示了新的Files.mismatch(Path,Path)并將其與Files.isSameFile(Path,Path)進行對比。
package dustin.examples.jdk12.files;import java.nio.file.Files; import java.nio.file.Path;import static java.lang.System.out;/*** Demonstrate {@code Files.mismatch(Path,Path)} introduced with JDK 12* and useful for determining if two files have the same content even* if they're not the same files.*/ public class FilesDemo {public static void main(final String[] arguments) throws Exception{if (arguments.length < 2){out.println("USAGE: FilesDemo <file1Name> <file2Name>");return;}final String file1Name = arguments[0];final Path file1Path = Path.of(file1Name);final String file2Name = arguments[1];final Path file2Path = Path.of(file2Name);out.println("\nFiles '" + file1Name + "' and '" + file2Name + "' are "+ (Files.isSameFile(file1Path, file2Path) ? "the" : "NOT the")+ " same.\n\n");out.println("\nFiles '" + file1Name + "' and '" + file2Name + "' are "+ (Files.mismatch(file1Path, file2Path) == -1 ? "the" : "NOT the")+ " same content.\n\n");} }當針對各種文件組合執(zhí)行上述代碼時,它將提供在下表中捕獲的結(jié)果。
| 同一文件 | true | true |
| 復制的文件 | false | true |
| 不同的文件 | false | false |
| 軟鏈接 | true | true |
| 硬連結(jié) | true | true |
添加Files.mismatch(Path,Path)是完成JDK-6852033 [“使常見的I / O任務(wù)更容易執(zhí)行的輸入/輸出方法”]的又一個步驟,它使確定兩個不相同文件的時間變得更容易。相同文件仍然“相等”或具有相同內(nèi)容。
翻譯自: https://www.javacodegeeks.com/2018/11/jdk-12s-files-mismatch-method.html
總結(jié)
以上是生活随笔為你收集整理的JDK 12的Files.mismatch方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring roo_使用Spring
- 下一篇: 安卓xbmc可以播放光盘(安卓xbmc)