AsynchronousFileChannel类的使用(二)

读取数据方式1

public abstract Future<Integer> read(ByteBuffer dst, long position)方法的作用是从给定的文件位置开始,从该通道将字节序列读人给定的缓冲区。此方法从给定的文件位置开始,将从该通道的字节序列读取到给定的缓冲区。此方法返回Future对象。如果给定位置大于或等于在尝试读取时文件的大小,则Future 的get()方法将返回读取的字节数或-1。
此方法的工作方式与AsynchronousByteChannel.read(ByteBuffer)方法相同,只是从给定文件位置开始读取字节。如果给定的文件位置大于文件在读取时的大小,则不读取任何字节。参数dst代表要将字节传输到的缓冲区。参数position代表开始的文件位置,必须是非负数。

a.txt文件的内容: 12345。

public static void main(String[] args) throws Exception{
        Path path = Paths.get("a.txt");
        AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        ByteBuffer byteBuffer = ByteBuffer.allocate(3);
        Future<Integer> future = channel.read(byteBuffer, 0);
        System.out.println("length=" +future.get());
        channel.close();
        byte[] byteArray = byteBuffer.array();
        for (int i = 0; i < byteArray.length; i++) {
            System.out.println((char)byteArray[i]);
        }
    }
length=3
1
2
3
读取数据方式2

a.txt文件的内容:12345

public static void main(String[] args) throws Exception{
        Path path = Paths.get("a.txt");
        AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        ByteBuffer byteBuffer = ByteBuffer.allocate(5);
        channel.read(byteBuffer, 0, "我是附加的参数", new CompletionHandler<Integer, String>() {
            @Override
            public void completed(Integer result, String attachment) {
                System.out.println("public void completed(Integer result, String attachment) result="
                    + result + " attachment=" + attachment);
            }

            @Override
            public void failed(Throwable exc, String attachment) {
                System.out.println("public void failed(Throwable exc, String attachment) attachment="
                    + attachment);
                System.out.println("getMessage=" + exc.getMessage());
            }
        });
        Thread.sleep(2000);
        channel.close();

        byte[] byteArray = byteBuffer.array();
        for (int i = 0; i < byteArray.length; i++) {
            System.out.println((char)byteArray[i]);
        }
    }
public void completed(Integer result, String attachment) result=5 attachment=我是附加的参数
1
2
3
4
5
写入数据方式1

a.txt文件的内容:12345

public static void main(String[] args) throws Exception{
        Path path = Paths.get("a.txt");
        AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
        ByteBuffer byteBuffer = ByteBuffer.wrap("abcde".getBytes());
        Future<Integer> future = channel.write(byteBuffer, channel.size());
        System.out.println("length=" + future.get());
        channel.close();
    }
length=5

a.txt追加写入:
在这里插入图片描述

写入数据方式2

a.txt文件的内容:12345

public static void main(String[] args) throws Exception{
        Path path = Paths.get("a.txt");
        AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
        ByteBuffer byteBuffer = ByteBuffer.wrap("abcde".getBytes());
        channel.write(byteBuffer, channel.size(), "我是附加的数据", new CompletionHandler<Integer, String>() {
            @Override
            public void completed(Integer result, String attachment) {
                System.out.println(" public void completed(Integer result, String attachment) result=" +
                        result + " attachment=" + attachment);
            }

            @Override
            public void failed(Throwable exc, String attachment) {
                System.out.println("public void failed(Throwable exc, String attachment) attachment=" +
                        attachment);
                System.out.println("getMessage=" + exc.getMessage());
            }
        });
        Thread.sleep(2000);
        channel.close();
    }
 public void completed(Integer result, String attachment) result=5 attachment=我是附加的数据

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值