国产探花免费观看_亚洲丰满少妇自慰呻吟_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/


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇州县| 宣武区| 兴义市| 灌云县| 岑溪市| 大竹县| 醴陵市| 搜索| 开封市| 仁布县| 岱山县| 新乡县| 沅陵县| 舟曲县| 阳新县| 廉江市| 黎平县| 博白县| 东山县| 怀远县| 安西县| 上思县| 顺义区| 封丘县| 广南县| 东阿县| 安多县| 兴化市| 开远市| 萨嘎县| 盐源县| 临城县| 固阳县| 祥云县| 扎囊县| 广东省| 揭阳市| 芦溪县| 禄丰县| 长子县| 华阴市|