Java NIO - 檔案鎖



眾所周知,Java NIO 支援併發和多執行緒,這使其能夠處理同時對多個檔案進行操作的多個執行緒。但在某些情況下,我們需要確保我們的檔案不會被任何執行緒共享,並且無法訪問。

對於這樣的需求,NIO 又提供了一個名為 FileLock 的 API,用於對整個檔案或檔案的一部分提供鎖,以便檔案或其部分不會被共享或訪問。

為了提供或應用此類鎖,我們必須使用 FileChannel 或 AsynchronousFileChannel,它們為此目的提供了兩種方法:lock()tryLock()。提供的鎖可以是兩種型別之一:

  • 獨佔鎖 - 獨佔鎖阻止其他程式獲取任何型別的重疊鎖。

  • 共享鎖 - 共享鎖阻止其他併發執行的程式獲取重疊的獨佔鎖,但允許它們獲取重疊的共享鎖。

用於獲取檔案鎖的方法:

  • lock() - FileChannel 或 AsynchronousFileChannel 的此方法獲取與給定通道關聯的檔案上的獨佔鎖。此方法的返回型別是 FileLock,可進一步用於監控獲得的鎖。

  • lock(long position, long size, boolean shared) - 此方法是 lock 方法的過載方法,用於鎖定檔案的特定部分。

  • tryLock() - 此方法返回一個 FileLock 或 null(如果無法獲取鎖),它嘗試在此通道的檔案上獲取顯式獨佔鎖。

  • tryLock(long position, long size, boolean shared) - 此方法嘗試在此通道檔案的給定區域上獲取鎖,該鎖可以是獨佔鎖或共享鎖。

FileLock 類的使用方法

  • acquiredBy() - 此方法返回獲取檔案鎖的通道。

  • position() - 此方法返回鎖定的區域第一個位元組在檔案中的位置。鎖定的區域不必包含在實際的基礎檔案中,甚至可能與之重疊,因此此方法返回的值可能超過檔案的當前大小。

  • size() - 此方法返回鎖定的區域大小(以位元組為單位)。鎖定的區域不必包含在實際的基礎檔案中,甚至可能與之重疊,因此此方法返回的值可能超過檔案的當前大小。

  • isShared() - 此方法用於確定鎖是否是共享鎖。

  • overlaps(long position,long size) - 此方法指示此鎖是否與給定的鎖定範圍重疊。

  • isValid() - 此方法指示獲得的鎖是否有效。鎖物件保持有效,直到它被釋放或關聯的檔案通道關閉,以先發生者為準。

  • release() - 釋放獲得的鎖。如果鎖物件有效,則呼叫此方法會釋放鎖並使物件無效。如果此鎖物件無效,則呼叫此方法不會產生任何影響。

  • close() - 此方法呼叫 release() 方法。新增此方法是為了使其能夠與自動資源管理塊構造一起使用。

演示檔案鎖的示例。

以下示例在檔案中建立一個鎖並向其中寫入內容。

package com.java.nio;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class FileLockExample {
   public static void main(String[] args) throws IOException {
      String input = "Demo text to be written in locked mode.";  
      System.out.println("Input string to the test file is: " + input);  
      ByteBuffer buf = ByteBuffer.wrap(input.getBytes());  
      String fp = "D:file.txt";  
      Path pt = Paths.get(fp);  
      FileChannel channel = FileChannel.open(pt, StandardOpenOption.WRITE,StandardOpenOption.APPEND);  
      channel.position(channel.size() - 1); // position of a cursor at the end of file       
      FileLock lock = channel.lock();   
      System.out.println("The Lock is shared: " + lock.isShared());  
      channel.write(buf);  
      channel.close(); // Releases the Lock  
      System.out.println("Content Writing is complete. Therefore close the channel and release the lock.");  
      PrintFileCreated.print(fp);  
   }  
}

package com.java.nio;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class PrintFileCreated {
   public static void print(String path) throws IOException {  
      FileReader filereader = new FileReader(path);  
      BufferedReader bufferedreader = new BufferedReader(filereader);  
      String tr = bufferedreader.readLine();    
      System.out.println("The Content of testout.txt file is: ");  
      while (tr != null) {      
         System.out.println("    " + tr);  
         tr = bufferedreader.readLine();  
      }  
   filereader.close();  
   bufferedreader.close();  
   }  
}

輸出

Input string to the test file is: Demo text to be written in locked mode.
The Lock is shared: false
Content Writing is complete. Therefore close the channel and release the lock.
The Content of testout.txt file is: 
To be or not to be?Demo text to be written in locked mode.
廣告