亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

更多QQ空间微信QQ好友腾讯朋友复制链接
您的位置:首頁/技術文章
文章詳情頁

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

【字号: 作者:豬豬瀏覽:5日期:2022-08-25 17:53:39

在Java 7,AsynchronousFileChannel 被添加到了Java NIO中。使用AsynchronousFileChannel可以實現異步地讀取和寫入文件數據。

創建一個AsynchronousFileChannel

我們可以使用AsynchronousFileChannel提供的靜態方法 open() 創建它。示例代碼如下:

Path path = Paths.get('data/test.xml');AsynchronousFileChannel fileChannel =AsynchronousFileChannel.open(path, StandardOpenOption.READ);

第一個參數是一個 PATH 的對像實例,它指向了那個與 AsynchronousFileChannel 相關聯的文件。

第二個參數是一個或多個操作選項,它決定了 AsynchronousFileChannel 將對目標文件做何種操作。示例代碼中我們使用了 StandardOpenOption.READ ,它表明我們將要對目標文件進行讀操作。

讀取數據

AsynchronousFileChannel 提供了兩種讀取數據的方式,都是調用它本身的 read() 方法。下面將對兩種方式進行介紹。

使用Futrue讀取數據

第一種反式是調用 AsynchronousFileChannel 的 read() 方法,該方法反回一個 Future 類型的對象。

Future operation = fileChannelread(buffer, 0);

第一個參數是ByteBuffer,從 AsynchronousFileChannel 中讀取的數據先寫入這個 ByteBuffer 。

第二個參數表示從文件讀取數據的開始位置。

此 read() 方法會立即返回,即使整個讀的過程還沒有完全結束。我們可以通過operation.isDone()來檢查讀取是否完成。這里的 operation 是上面調用 read() 方法返回的 Future 類型的實例。下面是一段詳細的代碼示例:

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

上面的程序首先創建了一個 AsynchronousFileChannel 對象,然后調用它的read()方法返回一個Future。其中read()方法需要兩個參數,一個是ByteBuffer,另一個是讀取文件的開始位置。然后通過循環調用isDone() 方法檢測讀取過程是否完成,完成后 isDone()方法將返回true。盡管這樣讓cpu空轉了一會,但是我們還是應該等讀取操作完成后再進行后續的步驟。

一旦讀取完成,數據被存儲到ByteBuffer,然后將數據轉化為字符串既而輸出。

使用CompletionHandler讀取數據

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

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() 方法將會被調用。它的第一個參數是個 Integer類型,表示讀取的字節數。第二個參數 attachment 是 ByteBuffer 類型的,用來存儲讀取的數據。它其實就是由 read() 方法的第三個參數。當前示例中,我們選用 ByteBuffer 來存儲數據,其實我們也可以選用其他的類型。

讀取失敗的時候,CompletionHandler的 failed()方法會被調用。

寫入數據

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

使用Future讀取數據

AsynchronousFileChannel也可以異步寫入數據。下面是一個完整的寫入示例:

AsynchronousFileChannel也可以異步寫入數據。下面是一個完整的寫入示例: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');

首先實例化一個寫入模式的 AsynchronousFileChannel, 然后創建一個 ByteBuffer 并寫入一些數據。再然后將數據寫入文件。最后,檢查返回的 Future,看是否寫入完成。

注意,寫入目標文件要提前創建好,如果它不存在的話,writh() 方法會拋出一個 java.nio.file.NoSuchFileException。

我們可以用以下方式來解決這一問題:

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

使用CompletionHandler寫入數據

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

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

當寫入程序完成時,CompletionHandler的completed()方法將會被調用,相反的如果寫入失敗則會調用failed()方法。

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 国产福利一区二区 | 精品国产免费人成网站 | 成人免费福利片在线观看 | 青青青国产视频 | 最新国产在线播放 | 欧美黄色免费大片 | 亚洲中国日本韩国美国毛片 | 亚洲欧美一区二区三区在线播放 | 日本在线三级 | 国产伦精品一区二区三区免费迷 | 亚洲国产一区在线观看 | 一区二区视频在线观看 | 三级黄色在线视频中文 | 亚洲最大网站 | 日韩免费高清一级毛片久久 | 久久久久久久91精品免费观看 | 欧美在线观看a | 国产在线视频www片 国产在线视频www色 | 亚洲欧美精品一区二区 | 免费黄色在线观看 | 国产亚洲欧美另类一区二区三区 | 91在线激情在线观看 | 日韩第一页在线 | 久久黄网| 欧美精品片 | 日韩在线一区二区三区视频 | 热久久国产欧美一区二区精品 | 国产乱码精品一区二区 | 成人做爰免费视频免费看 | 欧美毛片 | 国产一级二级三级 | 国产亚洲一区二区手机在线观看 | 多人性激烈的欧美三级视频 | 在线观看视频日韩 | 国产另类在线观看 | 亚欧日韩毛片在线看免费网站 | 久久综合狠狠综合久久 | 国产成人a大片大片在线播放 | 尤物视频在线观看视频 | 野外三级国产在线观看 | 国产闫盼盼私拍福利精品视频 |