Java NIO Channel to Channel Transfers(六)

原文链接:http://tutorials.jenkov.com/java-nio/channel-to-channel-transfers.html,如有侵权,立删

Java NIO Channel to Channel Transfers

  • transferFrom()
  • transferTo()

  如果两个channel中有一个是filechannel,那么两个channel就可以互相传输数据。

 TransferFrom()

  FileChannel.transferFrom()将一个channel中的数据传输到FileChannel中去。上例子

 1 RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
 2 FileChannel      fromChannel = fromFile.getChannel();
 3 
 4 RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
 5 FileChannel      toChannel = toFile.getChannel();
 6 
 7 long position = 0;
 8 long count    = fromChannel.size();
 9 
10 toChannel.transferFrom(fromChannel, position, count);

  一些SocketChannel实现可能只会传输已经准备好的数据,即使还有正在准备的很多可用数据。因此SocketChannel可能不会传输全部的数据到FileChannel中。

 TransferTo()

  FileChannel.transferTo()是FileChannel向其他的channel传输数据。

 1 RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
 2 FileChannel      fromChannel = fromFile.getChannel();
 3 
 4 RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
 5 FileChannel      toChannel = toFile.getChannel();
 6 
 7 long position = 0;
 8 long count    = fromChannel.size();
 9 
10 fromChannel.transferTo(position, count, toChannel);

  这里SocketChannel,还是老毛病。

转载于:https://www.cnblogs.com/AI-Cobe/p/10022982.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值