亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java NIO異步文件通道原理及用法解析

瀏覽:3日期:2022-08-25 17:53:39

在Java 7,AsynchronousFileChannel 被添加到了Java NIO中。使用AsynchronousFileChannel可以實(shí)現(xiàn)異步地讀取和寫入文件數(shù)據(jù)。

創(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)行讀操作。

讀取數(shù)據(jù)

AsynchronousFileChannel 提供了兩種讀取數(shù)據(jù)的方式,都是調(diào)用它本身的 read() 方法。下面將對(duì)兩種方式進(jìn)行介紹。

使用Futrue讀取數(shù)據(jù)

第一種反式是調(diào)用 AsynchronousFileChannel 的 read() 方法,該方法反回一個(gè) Future 類型的對(duì)象。

Future operation = fileChannelread(buffer, 0);

第一個(gè)參數(shù)是ByteBuffer,從 AsynchronousFileChannel 中讀取的數(shù)據(jù)先寫入這個(gè) ByteBuffer 。

第二個(gè)參數(shù)表示從文件讀取數(shù)據(jù)的開(kāi)始位置。

此 read() 方法會(huì)立即返回,即使整個(gè)讀的過(guò)程還沒(méi)有完全結(jié)束。我們可以通過(guò)operation.isDone()來(lái)檢查讀取是否完成。這里的 operation 是上面調(diào)用 read() 方法返回的 Future 類型的實(shí)例。下面是一段詳細(xì)的代碼示例:

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è)是讀取文件的開(kāi)始位置。然后通過(guò)循環(huán)調(diào)用isDone() 方法檢測(cè)讀取過(guò)程是否完成,完成后 isDone()方法將返回true。盡管這樣讓cpu空轉(zhuǎn)了一會(huì),但是我們還是應(yīng)該等讀取操作完成后再進(jìn)行后續(xù)的步驟。

一旦讀取完成,數(shù)據(jù)被存儲(chǔ)到ByteBuffer,然后將數(shù)據(jù)轉(zhuǎn)化為字符串既而輸出。

使用CompletionHandler讀取數(shù)據(jù)

第二種讀取數(shù)據(jù)的方式是調(diào)用AsynchronousFileChannel 的另一個(gè)重載 read() 方法,改方法需要一個(gè)CompletionHandler 作為參數(shù)。下面是代碼示例:

fileChannel.read(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() { @Override public 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(); } @Override public void failed(Throwable exc, ByteBuffer attachment) { }});

一旦讀取操作完成,CompletionHandler的 complete() 方法將會(huì)被調(diào)用。它的第一個(gè)參數(shù)是個(gè) Integer類型,表示讀取的字節(jié)數(shù)。第二個(gè)參數(shù) attachment 是 ByteBuffer 類型的,用來(lái)存儲(chǔ)讀取的數(shù)據(jù)。它其實(shí)就是由 read() 方法的第三個(gè)參數(shù)。當(dāng)前示例中,我們選用 ByteBuffer 來(lái)存儲(chǔ)數(shù)據(jù),其實(shí)我們也可以選用其他的類型。

讀取失敗的時(shí)候,CompletionHandler的 failed()方法會(huì)被調(diào)用。

寫入數(shù)據(jù)

就像讀取一樣,我們同樣有兩種方式向 AsynchronousFileChannel 寫入數(shù)據(jù)。我們可以調(diào)用它的2個(gè)重載的 write() 方法。下面我們將分別加以介紹。

使用Future讀取數(shù)據(jù)

AsynchronousFileChannel也可以異步寫入數(shù)據(jù)。下面是一個(gè)完整的寫入示例:

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。

我們可以用以下方式來(lái)解決這一問(wèn)題:

if(!Files.exists(path)){Files.createFile(path);}

使用CompletionHandler寫入數(shù)據(jù)

我們也可以使用 CompletionHandler代替Future向AsynchronousFileChannel寫入數(shù)據(jù),這種方式可以更加直接的知道寫入過(guò)程是否完成。下面是示例程序:

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>() { @Override public void completed(Integer result, ByteBuffer attachment) { System.out.println('bytes written: ' + result); } @Override public 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()方法。

要留意CompletionHandler的方法的參數(shù) attachemnt是怎么使用的。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 欧美成在人线a免费 | 日韩在线二区全免费 | 九九99九九视频在线观看 | 真人毛片免费全部播放完整 | 国产精品一区二区久久精品涩爱 | 福利姬在线精品观看 | 成人欧美视频 | 久久九九免费视频 | 欧美性色欧美a在线播放 | 老司机毛片 | 窝窝社区在线观看www | 91久久精品国产91久久性色tv | 免费一级做a爰片久久毛片 免费一级做a爰片性色毛片 | 成人在线a | a毛片免费播放全部完整 | 普通话对白国产精品一级毛片 | a国产成人免费视频 | 男女免费观看视频 | 亚洲热视频 | 四虎免费大片aⅴ入口 | 久久青草免费线观最新 | 中文字幕日韩精品有码视频 | 亚洲国产小视频 | 日韩性大片免费 | 国产日韩欧美在线观看不卡 | 国产成人a视频在线观看 | 加勒比一本大道香蕉在线视频 | 欧美日韩一本 | 免费永久在线观看黄网 | 在线观看国内自拍 | 成人免费视频网站 | 欧美一区二区二区 | 精品视频久久久久 | 欧美中文字幕一区二区三区 | 中文字幕波多野不卡一区 | 亚洲韩精品欧美一区二区三区 | 久久久综合网 | 欧美黄网站| 成年女人免费又黄又爽视频 | 午夜在线社区视频 | 国产成人综合高清在线观看 |