使用FileChannel复制文件

本文介绍如何使用Java的FileChannel进行文件传输,包括transferTo和transferFrom方法的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件通道定义了两个方法,可以进行直接的文件传输:
int transferTo(int position,long count,WritableByteChannel dst);
这个函数是把文件从position位置开始向dst通道传送count个字节。
int transferFrom(ReadableByteChannel src,long position,long count);
将count个字节从通道src传送到文件中,position是为文件开始写入的位置。
从FileInputStream中获得的通道只支持transferTo,而从FileOutputStream中获得的通道只支持tansferFrom()方法

  1. FileChannel sfc = new FileInputStream(“D://suqiang//from.txt”).getChannel();
  2. FileChannel tfc = new FileOutputStream(“D://suqiang//to.txt”).getChannel();
  3. sfc.transferTo(0, sfc.size(), tfc);
  4. sfc.close();
  5. tfc.close();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值