java nio接收文件_Java NIO(六)Channel 之间的转换(传输)

Java NIO 的 FileChannel 提供了 transferTo 和 transferFrom 方法,允许直接在通道间进行数据传输,简化了文件复制等操作。这两个方法分别用于从其他通道传输数据到 FileChannel 和从 FileChannel 传输数据到其他通道,但要注意 SocketChannel 在传输时可能会受内部缓冲区限制,不一定是全部数据。
摘要由CSDN通过智能技术生成

在Java NIO中,如果其中一个通道是FileChannel,则可以将数据直接从一个通道传输到另一个通道。 FileChannel类有一个transferTo()和一个transferFrom()方法,可以为你做到这一点。

transferFrom()

FileChannel.transferFrom()方法将来自源通道的数据传输到FileChannel中。 这是一个简单的例子:

RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");

FileChannel fromChannel = fromFile.getChannel();

RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");

FileChannel toChannel = toFile.getChannel();

long position = 0;

long count = fromChannel.size();

toChannel.transferFrom(fromChannel, position, count);

参数的位置和计数,告诉目的文件开始写入的位置(位置),以及最多传输多少字节(计数)。 如果源信道少于计数字节,则传输的信道少。

此外,一些SocketChannel实现可能只传递SocketChannel在其内部缓冲区中准备好的数据 - 即使SocketChannel稍后可能有更多的数据可用。 因此,它可能不会将从SocketChannel请求的所有数据(计数)传送到FileChannel。

transferTo()

transferTo()方法从FileChannel转移到其他通道。 这是一个简单的例子:

RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");

FileChannel fromChannel = fromFile.getChannel();

RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");

FileChannel toChannel = toFile.getChannel();

long position = 0;

long count = fromChannel.size();

fromChannel.transferTo(position, count, toChannel);

注意这个例子与前一个例子类似。 唯一真正的区别是调用该方法的FileChannel对象。 其余的是一样的。

SocketChannel的问题也出现在transferTo()方法中。 SocketChannel实现只能从FileChannel传输字节,直到发送缓冲区已满,然后停止。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值