当前位置:网站首页>使用 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();
}
}
}
边栏推荐
- [MySQL 13] if you change your password for the first time after installing mysql, you can skip MySQL password verification to log in
- PAT甲级 1027 Colors in Mars
- 什么是定义?什么是声明?它们有何区别?
- Redis batch startup and shutdown script
- experiment.........
- Robots protocol
- idea取消引用顯示效果
- 【cocos creator】获取资源uuid
- Oracle queries grouped by time
- Go language foundation ------ 14 ------ gotest
猜你喜欢

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

PAT甲级 1029 Median

haproxy+keepalived搭建01

Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction

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

Pat class a 1031 Hello world for u

Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select

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

Technical dry goods Shengsi mindspire dynamic transformer with variable sequence length has been released!
![[MySQL 12] MySQL 8.0.18 reinitialization](/img/e1/9874df18bbc8d80c3c5c5fe39aefc9.png)
[MySQL 12] MySQL 8.0.18 reinitialization
随机推荐
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
哪一刻你才发现青春结束了
MAE
What is definition? What is a statement? What is the difference between them?
Go language foundation ----- 02 ----- basic data types and operators
VMware virtual machine configuration static IP
Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition
*p++、*++p、++*p、(*p)++
Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
Enter three times and guess a number
Go language foundation ----- 11 ----- regular expression
[at] abc 258G - Triangle 三元組可達-暴力
GoLang之结构体
Robots protocol
Redis配置文件
Go language foundation ----- 05 ----- structure
【LeetCode】2. Valid Parentheses·有效的括号
[at] ABC 258g - triple Reach - violence
C language learning notes (mind map)
Technology dry goods | Roberta of the migration of mindspore NLP model - emotion analysis task