首页 > 学院 > 开发设计 > 正文

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/


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表