国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

AsynchronousFileChannel 文件操作

2019-11-08 03:15:23
字體:
來源:轉載
供稿:網友
創建文件 Future 讀取文件 CompletionHandler 讀取文件 Future 寫入文件CompletionHandler 寫入文件參考文章

創建文件

public void create(String path, String fileName) throws IOException { String uri = path + "/" + fileName; AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.CREATE, StandardOpenOption.WRITE);}

Future 讀取文件

public void readWithFuture(String path, String fileName) throws IOException { String uri = path + "/" + fileName; System.out.PRintln(uri); AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ); ByteBuffer buffer = ByteBuffer.allocate(1024); long position = 0; Future<Integer> Operation = channel.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();}

CompletionHandler 讀取文件

public void readWithCompletionHandler(String path, String fileName) throws IOException { String uri = path + "/" + fileName; System.out.println(uri); AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ); ByteBuffer buffer = ByteBuffer.allocate(1024); long position = 0; fileChannel.read(buffer, position, buffer, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, ByteBuffer buffer) { System.out.println("Read Done"); System.out.println("result = " + result); buffer.flip(); byte[] data = new byte[buffer.limit()]; buffer.get(data); System.out.println(new String(data)); buffer.clear(); } @Override public void failed(Throwable exc, ByteBuffer attachment) { System.out.println("Read failed"); exc.printStackTrace(); } }); // 給終端顯示留一些時間,實際項目可以刪除 int cTime = 0; while(cTime < 5) { try { Thread.sleep(500); ++cTime; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

Future 寫入文件

public void readWithFuture(String path, String fileName) throws IOException { String uri = path + "/" + fileName; System.out.println(uri); AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.READ); ByteBuffer buffer = ByteBuffer.allocate(1024); long position = 0; Future<Integer> operation = channel.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(); }

CompletionHandler 寫入文件

public void writeWithCompletionHandler(String path, String fileName, String message) throws IOException { String uri = path + "/" + fileName; System.out.println(uri); final AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(uri), StandardOpenOption.CREATE, StandardOpenOption.WRITE); byte[] byteArray = message.getBytes(); ByteBuffer buffer = ByteBuffer.wrap(byteArray); channel.write(buffer, 0, null, new CompletionHandler<Integer, ByteBuffer>(){ @Override public void completed(Integer result, ByteBuffer attachment) { System.out.println("Write done"); } @Override public void failed(Throwable exc, ByteBuffer attachment) { System.out.println("Write failed"); exc.printStackTrace(); } }); }

參考文章

java NIO:http://tutorials.jenkov.com/java-nio/asynchronousfilechannel.html String ByteBuffer轉換:http://ivan4126.blog.163.com/blog/static/209491092201361621344523/


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遂宁市| 罗定市| 噶尔县| 略阳县| 白城市| 邢台市| 方山县| 获嘉县| 石泉县| 平安县| 远安县| 宁武县| 通许县| 武平县| 华宁县| 广东省| 保康县| 长顺县| 株洲县| 静海县| 江津市| 西吉县| 泌阳县| 前郭尔| 安达市| 光山县| 焦作市| 常山县| 泾源县| 蒲江县| 定远县| 吴江市| 正阳县| 芮城县| 德化县| 清流县| 西乌| 逊克县| 阿荣旗| 绥芬河市| 鄄城县|