NIO详解(十二):AsynchronousFileChannel详解
1. 概述
Java NIO中的FileChannel是一個(gè)連接到文件的通道。可以通過文件通道讀寫文件。FileChannel無法設(shè)置為非阻塞模式,他總是運(yùn)行在阻塞模式下。在Java 7中,AsynchronousFileChannel被添加到Java NIO。AsynchronousFileChannel使讀取數(shù)據(jù),并異步地將數(shù)據(jù)寫入文件成為可能。
2. 創(chuàng)建一個(gè)AsynchronousFileChannel
使用AsynchronousFileChannel提供的靜態(tài)方法 open() 創(chuàng)建它。示例代碼如下:
Path path = Paths.get("data/test.xml"); AsynchronousFileChannel fileChannel =AsynchronousFileChannel.open(path, StandardOpenOption.READ);第一個(gè)參數(shù)是一個(gè) PATH 的對(duì)象實(shí)例,它指向了那個(gè)與 AsynchronousFileChannel 相關(guān)聯(lián)的文件。
第二個(gè)參數(shù)是一個(gè)或多個(gè)操作選項(xiàng),它決定了 AsynchronousFileChannel 將對(duì)目標(biāo)文件做何種操作。示例代碼中我們使用了 StandardOpenOption.READ ,它表明我們將要對(duì)目標(biāo)文件進(jìn)行讀操作。
3. 從AsynchronousFileChannel中讀取數(shù)據(jù)
3.1 使用Futrue讀取數(shù)據(jù)
從AsynchronousFileChannel讀取數(shù)據(jù)的第一種方法是調(diào)用返回Future的read()方法。下面是如何調(diào)用這個(gè)read()方法的示例:
Future<Integer> operation = fileChannel.read(buffer, 0);read()方法的這個(gè)版本將ByteBuffer作為第一個(gè)參數(shù)。從AsynchronousFileChannel讀取的數(shù)據(jù)被讀入這個(gè)ByteBuffer。第二個(gè)參數(shù)是文件中的字節(jié)位置,以便開始讀取。
read()方法會(huì)立即返回,即使讀操作還沒有完成。通過調(diào)用read()方法返回的Future實(shí)例的isDone()方法,您可以檢查讀取操作是否完成。
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);ByteBuffer buffer = ByteBuffer.allocate(1024); long position = 0;Future<Integer> operation = fileChannel.read(buffer, position);while(!operation.isDone());buffer.flip(); byte[] data = new byte[buffer.limit()]; buffer.get(data); System.out.println(new String(data)); buffer.clear();上面的程序首先創(chuàng)建了一個(gè) AsynchronousFileChannel 對(duì)象,然后調(diào)用它的read()方法返回一個(gè)Future。其中read()方法需要兩個(gè)參數(shù),一個(gè)是ByteBuffer,另一個(gè)是讀取文件的開始位置。然后通過循環(huán)調(diào)用isDone() 方法檢測(cè)讀取過程是否完成,完成后 isDone()方法將返回true。盡管這樣讓cpu空轉(zhuǎn)了一會(huì),但是我們還是應(yīng)該等讀取操作完成后再進(jìn)行后續(xù)的步驟。
一旦讀取完成,數(shù)據(jù)被存儲(chǔ)到ByteBuffer,然后將數(shù)據(jù)轉(zhuǎn)化為字符串既而輸出。
3.2 使用CompletionHandler讀取數(shù)據(jù)
第二種讀取數(shù)據(jù)的方式是調(diào)用AsynchronousFileChannel 的另一個(gè)重載 read() 方法,改方法需要一個(gè)CompletionHandler 作為參數(shù)。下面是代碼示例:
fileChannel.read(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() {@Overridepublic void completed(Integer result, ByteBuffer attachment) {System.out.println("result = " + result);attachment.flip();byte[] data = new byte[attachment.limit()];attachment.get(data);System.out.println(new String(data));attachment.clear();}@Overridepublic void failed(Throwable exc, ByteBuffer attachment) {} });一旦讀取操作完成,CompletionHandler的 complete() 方法將會(huì)被調(diào)用。它的第一個(gè)參數(shù)是個(gè) Integer類型,表示讀取的字節(jié)數(shù)。第二個(gè)參數(shù) attachment 是 ByteBuffer 類型的,用來存儲(chǔ)讀取的數(shù)據(jù)。它其實(shí)就是由 read() 方法的第三個(gè)參數(shù)。當(dāng)前示例中,我們選用 ByteBuffer 來存儲(chǔ)數(shù)據(jù),其實(shí)我們也可以選用其他的類型。讀取失敗的時(shí)候,CompletionHandler的 failed()方法會(huì)被調(diào)用。
4. 向AsynchronousFileChannel中寫入數(shù)據(jù)
就像讀取一樣,我們同樣有兩種方式向 AsynchronousFileChannel 寫入數(shù)據(jù)。我們可以調(diào)用它的2個(gè)重載的 write() 方法。下面我們將分別加以介紹。
4.1 使用Future讀取數(shù)據(jù)
AsynchronousFileChannel也可以異步寫入數(shù)據(jù)。下面是一個(gè)完整的寫入示例:
Path path = Paths.get("data/test-write.txt"); AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);ByteBuffer buffer = ByteBuffer.allocate(1024); long position = 0;buffer.put("test data".getBytes()); buffer.flip();Future<Integer> operation = fileChannel.write(buffer, position); buffer.clear();while(!operation.isDone());System.out.println("Write done");首先實(shí)例化一個(gè)寫入模式的 AsynchronousFileChannel, 然后創(chuàng)建一個(gè) ByteBuffer 并寫入一些數(shù)據(jù)。再然后將數(shù)據(jù)寫入文件。最后,檢查返回的 Future,看是否寫入完成。
注意,寫入目標(biāo)文件要提前創(chuàng)建好,如果它不存在的話,writh() 方法會(huì)拋出一個(gè) java.nio.file.NoSuchFileException。
4.2 使用CompletionHandler寫入數(shù)據(jù)
使用 CompletionHandler代替Future向AsynchronousFileChannel寫入數(shù)據(jù),這種方式可以更加直接的知道寫入過程是否完成。下面是示例程序:
Path path = Paths.get("data/test-write.txt"); if(!Files.exists(path)){Files.createFile(path); } AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);ByteBuffer buffer = ByteBuffer.allocate(1024); long position = 0;buffer.put("test data".getBytes()); buffer.flip();fileChannel.write(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() {@Overridepublic void completed(Integer result, ByteBuffer attachment) {System.out.println("bytes written: " + result);}@Overridepublic void failed(Throwable exc, ByteBuffer attachment) {System.out.println("Write failed");exc.printStackTrace();} });當(dāng)寫入程序完成時(shí),CompletionHandler的completed()方法將會(huì)被調(diào)用,相反的如果寫入失敗則會(huì)調(diào)用failed()方法。
總結(jié)
以上是生活随笔為你收集整理的NIO详解(十二):AsynchronousFileChannel详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NIO详解(十一):线程间通信管道Pip
- 下一篇: NIO详解(十三):Java IO 和N