java 解锁关闭文件占用_程序员:Java文件锁定、解锁和其它NIO操作

文件锁

java中i/o的文件锁定有两种:一种是独占锁,一种是共享锁。

共享锁既是共享读操作,但是只有一个可以进行写操作,共享锁防止其他正在运行的程序获取重复的独占锁,但是允许其他程序可以获取共享锁。

独占锁既只允许程序获取一个锁,独占锁防止程序获取其他的任何锁。

样例

样例基于Java的IO和NIO编写,分别对这个RandomAccessFile对象生成的FileChannel对象加锁/释放锁。

为保证锁的释放,需要finally块中进行处理。

前面有建立ByteBuffer,后面则是将ByteBuffer对象转成CharBuffer对象从而在控制台打印出来~~

详细说明见代码,大多数注释都是用嘤文加的~~

import java.io.*;

import java.nio.*;

import java.nio.channels.*;

import java.nio.charset.*;

public class LockingFileExample {

//排它/互斥

public static final boolean EXCLUSIVE = false;

//共享

public static final boolean SHARED = true;

//文件路径

private static final String FILE_PATH = "src/com/neu/algorithm/os_file/file.txt";

public static void main(String[] args) throws IOException {

//共享锁

FileLock sharedLock = null;

//排它锁/互斥锁

FileLock exclusiveLock = null;

try {

RandomAccessFile raf = new RandomAccessFile(FILE_PATH, "rw");

//get the channel from the file

FileChannel ch = raf.getChannel();

ByteBuffer buffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());

//this locks the first half of the file - exclusive

exclusiveLock = ch.lock(0, raf.length()/2, EXCLUSIVE);

/* Now modify the data . . . */

ch.position(raf.length());

ch.write(buffer);

//release the lock

exclusiveLock.release();

//this locks the second half of the file - shared

sharedLock = ch.lock(raf.length()/2+1, raf.length(), SHARED);

/* Now read the data . . . */

ByteBuffer buffer2 = ch.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());

ch.read(buffer2);

//release the lock

sharedLock.release();

buffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());

//create the UTF-8 decoder

Charset charset = Charset.forName("UTF-8");

CharsetDecoder charsetDecoder = charset.newDecoder();

//ByteBuffer -> CharBuffer

CharBuffer charBuffer = charsetDecoder.decode(buffer);

//output

System.out.println(charBuffer);

} catch (IOException ioe) {

ioe.printStackTrace();

} finally {

if (exclusiveLock != null) {

exclusiveLock.release();

}

if (sharedLock != null) {

sharedLock.release();

}

}

}

}

文件内容

Loving him is like driving a new Maserati down a dead end street

Faster than the wind, passionate as sin, ending so suddenly

Loving him is like trying to change your mind once you’re already flying through the free fall

Like the colors in autumn, so bright, just before they lose it all

Losing him was blue, like I’ve never known

Missing him was dark grey, all alone

Forgetting him was like trying to know somebody you never met

But loving him was red

Loving him was red

Touching him was like realizing all you ever wanted was right there in front of you

Memorizing him was as easy as knowing all the words to your old favorite song

Fighting with him was like trying to solve a crossword and realizing there’s no right answer

Regretting him was like wishing you’d never found out that love could be that strong

Losing him was blue, like I’ve never known

Missing him was dark grey, all alone

Forgetting him was like trying to know somebody you never met

But loving him was red

Oh, red burning red

Remembering him comes in flashbacks, in echoes

Tell myself it’s time now, gotta let go

But moving on from him is impossible when I still see it all in my head

Burning red

Loving him was red

Losing him was blue, like I’ve never known

Missing him was dark grey, all alone

Forgetting him was like trying to know somebody you never met

‘Cause loving him was red

Yeah, yeah red

burning red

And that’s why he’s spinning round in my head

Comes back to me, burning red

Yeah, yeah

His love was like driving a new Maserati down a dead end street

运行效果

79a85e9bc8c9475756004767fa5b95b1.png

Console打印文件内容:

b75f35af7e17d9cbefd36622f13dd46e.png

文件

ec3f60fd5a290a6505077fc0a408cc6f.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值