当前位置:网站首页>使用 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();
}
}
}
边栏推荐
- 创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
- Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
- Pat class a 1028 list sorting
- How to clear the console password for s7700 device
- Go language - loop statement
- Go language foundation ----- 04 ----- closure, array slice, map, package
- 基于RNA的新型癌症疗法介绍
- *p++、*++p、++*p、(*p)++
- What is a data type? What is the use of data types?
- 什麼是定義?什麼是聲明?它們有何區別?
猜你喜欢

vcs import src < ros2. Repos failed

Shengsi mindspire is upgraded again, the ultimate innovation of deep scientific computing

Project experience sharing: realize an IR Fusion optimization pass of Shengsi mindspire layer

【MySQL 12】MySQL 8.0.18 重新初始化

Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does

Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire

Technology dry goods | Roberta of the migration of mindspore NLP model - emotion analysis task

Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once

Professor Zhang Yang of the University of Michigan is employed as a visiting professor of Shanghai Jiaotong University, China (picture)

Go language foundation ------ 12 ------ JSON
随机推荐
vcs import src < ros2. Repos failed
Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select
【MySQL 12】MySQL 8.0.18 重新初始化
PHP common sorting algorithm
Go language foundation ----- 07 ----- method
【cocos creator】获取资源uuid
idea取消引用顯示效果
【LeetCode】2. Valid parentheses · valid parentheses
Precautions for opensips and TLS SIP trunk docking
一篇文章让你读懂-曼彻斯特编码
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
[at] abc 258G - Triangle 三元組可達-暴力
Go language foundation ----- 13 ----- file
Go language foundation ----- 08 ----- interface
Iterm2 setting
截图工具Snipaste
【MySQL 13】安装MySQL后第一次修改密码,可以可跳过MySQL密码验证进行登录
Pat class a 1030 travel plan
VMware virtual machine configuration static IP