java nio文件锁_Java NIO通道FileLock

FileLock锁定或尝试锁定文件的给定部分。它属于java.nio.channels包,该功能在JDK 1.4以上版本可用。

FileLock用于在共享模式或非共享模式下锁定文件。它有两个重要的方法如下:

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

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

上述方法使用参数作为初始位置,文件大小锁定和一个参数来决定是否共享锁定。

创建文件锁

当使用FileChannel或AsynchronousFileChannel的lock()或tryLock()方法之一获取文件锁时,将创建文件锁定对象。

基本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文件的内容如下 -

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();

}

}

注意:在运行代码之前,需要创建一个名称为“testout-file.txt”的文本文件,文本文件的内容如下:

Welcome to yiibai.com

This is the example of FileLock in Java NIO channel.

执行上面示例代码,得到以下结果 -

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.

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值