AsynchronousFileChannel 文件操作

  • 创建文件
  • 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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值