当前位置:网站首页>使用 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();
}
}
}
边栏推荐
- Pat grade a 1027 colors in Mars
- Go language foundation ----- 09 ----- exception handling (error, panic, recover)
- 基于RNA的新型癌症疗法介绍
- Screenshot tool snipaste
- [MySQL 13] if you change your password for the first time after installing mysql, you can skip MySQL password verification to log in
- Project experience sharing: Based on mindspore, the acoustic model is realized by using dfcnn and CTC loss function
- Huawei switches are configured with SSH login remote management switches
- PDO and SDO concepts
- [MySQL 12] MySQL 8.0.18 reinitialization
- STM32F103 SPI (pit Diary)
猜你喜欢

Go language foundation ------ 14 ------ gotest

Project experience sharing: Based on mindspore, the acoustic model is realized by using dfcnn and CTC loss function

Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition

Usage of requests module

Oracle queries grouped by time

Pat class a 1032 sharing

Pat grade a 1029 median

【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题

Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico

密西根大学张阳教授受聘中国上海交通大学客座教授(图)
随机推荐
EtherCAT state machine transition (ESM)
Iterm2设置
Precautions for opensips and TLS SIP trunk docking
Huawei s5700 switch initialization and configuration Telnet, SSH user methods
Go language foundation ----- 02 ----- basic data types and operators
Go language foundation ------ 12 ------ JSON
微软安全响应中心
OSPF protocol summary
experiment.........
【LeetCode】4. Best time to buy and sell stock
PAT甲级 1027 Colors in Mars
Redis批量启停脚本
Go language foundation ------ 14 ------ gotest
HDMI2.1与HDMI2.0的区别以及转换PD信号。
Client server model
Go language foundation ----- 19 ----- context usage principle, interface, derived context (the multiplexing of select can be better understood here)
【踩坑系列】mysql 修改root密码失败
Getting started with minicom
MAE
Uniapp learning records