当前位置:网站首页>使用 FileChannel 进行文件的复制拷贝
使用 FileChannel 进行文件的复制拷贝
2022-07-03 07:53:00 【雁迟】
package com.yanchi;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/** * @author yanchi * @date 2022/6/30 14:04 */
public class FileChannelTest {
public static void main(String[] args) {
try (
FileChannel from = new FileInputStream("data.txt").getChannel();
FileChannel to = new FileOutputStream("to.txt").getChannel();
) {
// 一次最高只能传输 2g
from.transferTo(0,from.size(),to);
} catch (IOException e) {
e.printStackTrace();
}
}
}
由于以上方法最大只能传输 2G 的文件,下面的代码对以上代码进行优化!!
package com.yanchi;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/** * @author yanchi * @date 2022/6/30 14:04 */
public class FileChannelTest {
public static void main(String[] args) {
try (
FileChannel from = new FileInputStream("data.txt").getChannel();
FileChannel to = new FileOutputStream("to.txt").getChannel();
) {
// 源文件字节数
long size = from.size();
// left 代表还剩下多少字节
for (long left = size; left > 0;){
left -= from.transferTo((size - left), left, to);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
边栏推荐
- PHP微信抢红包的算法
- 【cocos creator】获取资源uuid
- 华为交换机:配置telnet和ssh、web访问
- Redis batch startup and shutdown script
- 研究显示乳腺癌细胞更容易在患者睡觉时进入血液
- Go language foundation ------ 14 ------ gotest
- Iterm2 setting
- E: 无法定位软件包 ros-melodic-desktop-full
- Pat class a 1031 Hello world for u
- PIP uses image website to solve the problem of slow network speed
猜你喜欢

Go language foundation ----- 07 ----- method

STM32F103 SPI (pit Diary)

Go language foundation ----- 06 ----- anonymous fields, fields with the same name

vcs import src < ros2. Repos failed

Research shows that breast cancer cells are more likely to enter the blood when patients sleep

一个实习生的CnosDB之旅

Go language foundation ----- 05 ----- structure

Robots protocol

HDMI2.1与HDMI2.0的区别以及转换PD信号。

haproxy+keepalived搭建01
随机推荐
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
Go language foundation ----- 11 ----- regular expression
Idea dereference display effect
RM delete file
Uniapp learning records
Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
Oracle queries grouped by time
【MySQL 12】MySQL 8.0.18 重新初始化
【MySQL 13】安装MySQL后第一次修改密码,可以可跳过MySQL密码验证进行登录
[at] abc 258G - Triangle 三元組可達-暴力
Redis配置文件
How to clear the console password for s7700 device
yarn link 是如何帮助开发者对 NPM 包进行 debug 的?
static关键字
Harmonyos third training notes
Register keyword
【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
PHP微信抢红包的算法
Pat class a 1028 list sorting
LwIP learning socket (API)