Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API 和框架

Java 類引用

Java 有用資源

Java - Files mismatch() 方法



Files mismatch() 方法

mismatch() 方法從 Java 12 開始在 Files 中可用。它提供了一種簡單的方法來比較兩個檔案。

語法

Files.mismatch() 方法的語法如下:

public static long mismatch(Path path, Path path2) throws IOException

其中

  • 如果沒有不匹配,則返回 1L,否則返回第一個不匹配的位置。

  • 如果檔案大小不匹配或位元組內容不匹配,則會考慮不匹配。

在以下情況下,檔案被認為是相同的。

  • 如果兩個位置都指向同一個檔案。

  • 如果路徑相同且檔案不存在,則檔案被認為是相同的。

  • 如果檔案大小相同,並且第一個檔案的每個位元組都與第二個檔案的相應位元組匹配。

引數

path - 第一個檔案路徑

path2 - 第二個檔案路徑

返回值

第一個不匹配的位置,如果不存在不匹配,則返回 -1L。

異常

IOException - 如果發生 I/O 錯誤。

SecurityException - 在預設提供程式的情況下,如果安裝了安全管理器,則會呼叫 checkRead() 方法以檢查對兩個檔案的讀取訪問許可權。

Files mismatch() 方法示例

示例:檔案沒有不匹配

在此示例中,我們在臨時目錄中建立了兩個 txt 檔案,然後將相同的內容寫入這兩個檔案。使用 Files.mismatch() 方法,比較這兩個檔案是否相同。根據返回的結果,我們列印訊息。由於兩個檔案內容相同,因此將列印訊息“檔案匹配”。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class APITester {
   public static void main(String[] args) throws IOException {
      // create two files in temp directory   
      Path path1 = Files.createTempFile("file1", ".txt");
      Path path2 = Files.createTempFile("file2", ".txt");

      // write same content to both the files
      Files.writeString(path1, "tutorialspoint");
      Files.writeString(path2, "tutorialspoint");

      // check files for Mismatch, 
      // being same content, it should return -1.
      long mismatch = Files.mismatch(path1, path2);

      // print the message based on mismatch result
      if(mismatch > 1L) {
         System.out.println("Mismatch occurred in file1 and file2 at : " + mismatch);
      }else {
         System.out.println("Files matched");
      }
	  
      // delete the files	  
      path1.toFile().deleteOnExit();
      path2.toFile().deleteOnExit();
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Files matched

示例:檔案中的不匹配識別

在此示例中,我們在臨時目錄中建立了兩個 txt 檔案,然後將不同的內容寫入這兩個檔案。使用 Files.mismatch() 方法,比較這兩個檔案是否相同。根據返回的結果,我們列印訊息。由於兩個檔案內容不同,因此不匹配方法返回第一個不匹配的位置,該位置將作為輸出列印。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class APITester {
   public static void main(String[] args) throws IOException {
      // create two files in temp directory   
      Path path1 = Files.createTempFile("file1", ".txt");
      Path path2 = Files.createTempFile("file2", ".txt");

      // write same content to both the files
      Files.writeString(path1, "tutorialspoint");
      Files.writeString(path2, "tutorialspoint Java 12");

      // check files for Mismatch, 
      // being different content, it should return index of first mismatch.
      long mismatch = Files.mismatch(path1, path2);

      // print the message based on mismatch result
      if(mismatch > 1L) {
         System.out.println("Mismatch occurred in file1 and file2 at : " + mismatch);
      }else {
         System.out.println("Files matched");
      }
	  
      // delete the files	  
      path1.toFile().deleteOnExit();
      path2.toFile().deleteOnExit();
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Mismatch occurred in file1 and file3 at : 14
廣告