📜  mismatch() Java.nio.file.Files Utility 类的方法和例子

📅  最后修改于: 2022-05-13 01:55:32.320000             🧑  作者: Mango

mismatch() Java.nio.file.Files Utility 类的方法和例子

在Java jdk-12 的Java.nio.file.Files 类中新增了mismatch() 方法。该方法主要用于比较两个文件从头开始的每个字节数据,返回位不同的点。如果该位在任何时候都没有不同,则它返回“-1L”,表明文件是相同的,并且是彼此的精确副本。

该方法主要以文件的路径为输入,返回一个表示位不匹配位置的long值。所以这个方法的声明如下所示:

long position = Files.mismatch(Path path1, Path path2);

示例 1:

文件是:

Java
// Java program to demonstrate the usage
// of mismatch() method
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
 
// save file as the name of GFG2
public class GFG2 {
   
    // main class
    public static void main(String[] args) throws Exception
    {
        // getting the file path to the respective two files
        Path filePath1 = Path.of("C:\\Users\\Dipak\\Desktop\\gfg.txt");
        Path filePath2 = Path.of("c:\\Users\\Dipak\\Desktop\\gfg2.txt");
       
        // calling the mismatchfunction
        long mis_match = Files.mismatch(filePath1, filePath2);
       
        // printing the output result.
        if (mis_match == -1)
            System.out.println("No mismatch found in the files");
        else
            System.out.println("mismatch found");
    }
}


Java
// Java program to demonstrate the
// usage of mismatch() method
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
 
// save file as the name of GFG2
public class GFG2 {
 
    // main class
    public static void main(String[] args) throws Exception
    {
        // getting the file path to the respective two files
        Path filePath1 = Path.of("C:\\Users\\Dipak\\Desktop\\gfg.txt");
        Path filePath2 = Path.of("c:\\Users\\Dipak\\Desktop\\gfg2.txt");
       
        // calling the mismatchfunction
        long mis_match = Files.mismatch(filePath1, filePath2);
       
        // printing the output result.
        if (mis_match == -1)
            System.out.println("No mismach found in the files");
        else
            System.out.println("mismatch found");
    }
}


输出:

示例 2:

文件是:

Java

// Java program to demonstrate the
// usage of mismatch() method
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
 
// save file as the name of GFG2
public class GFG2 {
 
    // main class
    public static void main(String[] args) throws Exception
    {
        // getting the file path to the respective two files
        Path filePath1 = Path.of("C:\\Users\\Dipak\\Desktop\\gfg.txt");
        Path filePath2 = Path.of("c:\\Users\\Dipak\\Desktop\\gfg2.txt");
       
        // calling the mismatchfunction
        long mis_match = Files.mismatch(filePath1, filePath2);
       
        // printing the output result.
        if (mis_match == -1)
            System.out.println("No mismach found in the files");
        else
            System.out.println("mismatch found");
    }
}

输出:

输出