鍍金池/ 教程/ Java/ Java NIO通道FileLock
Java NIO選擇器
Java NIO ServerSocketChannel
Java NIO通道FileLock
Java NIO組件
Java NIO編碼和解碼
Java NIO包
Java NIO緩沖區(qū)
Java NIO教程
Java NIO SocketChannel
Java NIO時(shí)間服務(wù)器示例
Java NIO字符集
Java NIO通道之間的數(shù)據(jù)傳輸
Java NIO通道
Java IO與NIO比較
Java NIO管道
Java NIO分散/聚集或向量I/O

Java NIO通道FileLock

FileLock鎖定或嘗試鎖定文件的給定部分。它屬于java.nio.channels包,該功能在JDK 1.4以上版本可用。

FileLock用于在共享模式或非共享模式下鎖定文件。它有兩個(gè)重要的方法如下:

  • FileLock.lock(long position, long size, boolean shared)
  • FileLock.tryLock(long position, long size, boolean shared)

上述方法使用參數(shù)作為初始位置,文件大小鎖定和一個(gè)參數(shù)來決定是否共享鎖定。

創(chuàng)建文件鎖

當(dāng)使用FileChannelAsynchronousFileChannellock()tryLock()方法之一獲取文件鎖時(shí),將創(chuàng)建文件鎖定對象。

基本FileLock示例

下面來看看使用專用鎖定的通道在文件中寫入(附加)的程序(FileLockExample.java):

package com.yiibai;

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.ByteBuffer;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class FileLockExample {
    public static void main (String [] args)  
            throws IOException {  
        String input = "* end of the file.";  
        System.out.println("Input string to the test file is: " + input);  
        ByteBuffer buf = ByteBuffer.wrap(input.getBytes());  
        String fp = "testout-file.txt";  
        Path pt = Paths.get(fp);  
        FileChannel fc = FileChannel.open(pt, StandardOpenOption.WRITE,  
StandardOpenOption.APPEND);  
        System.out.println("File channel is open for write and Acquiring lock...");  
        fc.position(fc.size() - 1); // position of a cursor at the end of file       
        FileLock lock = fc.lock();   
        System.out.println("The Lock is shared: " + lock.isShared());  
        fc.write(buf);  
        fc.close(); // Releases the Lock  
        System.out.println("Content Writing is complete. Therefore close the channel and release the lock.");  
        PrintFile.print(fp);  
    }
}

PrintFile.java文件的內(nèi)容如下 -

package com.yiibai;

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class PrintFile {
    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-file.txt file is: ");
        while (tr != null) {
            System.out.println("    " + tr);
            tr = bufferedreader.readLine();
        }
        filereader.close();
        bufferedreader.close();
    }
}

注意:在運(yùn)行代碼之前,需要創(chuàng)建一個(gè)名稱為“testout-file.txt”的文本文件,文本文件的內(nèi)容如下:

Welcome to yiibai.com

This is the example of FileLock in Java NIO channel.

執(zhí)行上面示例代碼,得到以下結(jié)果 -

Input string to the test file is: * end of the file.
File channel is open for write and Acquiring lock...
The Lock is shared: false
Content Writing is complete. Therefore close the channel and release the lock.
The Content of testout-file.txt file is: 
    Welcome to yiibai.com

    This is the example of FileLock in Java NIO channel.* end of the file.