【Java】文件锁定与系列NIO操作
生活随笔
收集整理的這篇文章主要介紹了
【Java】文件锁定与系列NIO操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
Java中,獲取文件鎖的API需要訪問文件的FileChannel。
lock()可以獲得鎖,release()可以釋放鎖。
詳見API文檔
樣例
樣例基于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/.../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 fileFileChannel ch = raf.getChannel();ByteBuffer buffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());//this locks the first half of the file - exclusiveexclusiveLock = ch.lock(0, raf.length()/2, EXCLUSIVE);/* Now modify the data . . . */ch.position(raf.length());ch.write(buffer);//release the lockexclusiveLock.release();//this locks the second half of the file - sharedsharedLock = 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 locksharedLock.release();buffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());//create the UTF-8 decoderCharset charset = Charset.forName("UTF-8");CharsetDecoder charsetDecoder = charset.newDecoder();//ByteBuffer -> CharBufferCharBuffer charBuffer = charsetDecoder.decode(buffer);//outputSystem.out.println(charBuffer);} catch (IOException ioe) {ioe.printStackTrace();} finally {if (exclusiveLock != null) {exclusiveLock.release();}if (sharedLock != null) {sharedLock.release();}}} }文件內容
霉霉的《Red》,很喜歡的一首歌~~
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運行效果
【原文件】
【Console】
打印文件內容:
【文件】
總結
以上是生活随笔為你收集整理的【Java】文件锁定与系列NIO操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三角形分类(洛谷P5717题题解,Jav
- 下一篇: 【Python】Matplotlib绘制