当前位置:网站首页>使用 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甲级 1027 Colors in Mars
- C language learning notes (mind map)
- idea取消引用顯示效果
- PAT甲级 1031 Hello World for U
- Go language foundation ----- 10 ----- string related operations (operation function, string conversion)
- PHP common sorting algorithm
- Huawei switches are configured with SSH login remote management switches
- 研究显示乳腺癌细胞更容易在患者睡觉时进入血液
- 什麼是定義?什麼是聲明?它們有何區別?
- Go language foundation ----- 09 ----- exception handling (error, panic, recover)
猜你喜欢
Technical dry goods | some thoughts on the future of AI architecture
【LeetCode】2. Valid Parentheses·有效的括号
Redis batch startup and shutdown script
MAE
Install cross compiler arm none liunx gnueabihf
PAT甲级 1030 Travel Plan
Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward
haproxy+keepalived搭建01
Research shows that breast cancer cells are more likely to enter the blood when patients sleep
【LeetCode】4. Best time to buy and sell stock
随机推荐
哪一刻你才发现青春结束了
PAT甲级 1032 Sharing
Go language foundation ----- 11 ----- regular expression
Go language foundation ----- 13 ----- file
基于RNA的新型癌症疗法介绍
What is a data type? What is the use of data types?
Huawei switch console password reset, device initialization, default password
Project experience sharing: Based on mindspore, the acoustic model is realized by using dfcnn and CTC loss function
PHP wechat red packet grabbing algorithm
Go language foundation ----- 15 ----- reflection
Wechat native applet cloud development learning record 01
微软安全响应中心
Go language foundation ----- 08 ----- interface
Screenshot tool snipaste
JS common basic case sorting (continuous update)
tp3.2和tp5.0的区别
Go language foundation ------ 14 ------ gotest
[MySQL 13] if you change your password for the first time after installing mysql, you can skip MySQL password verification to log in
Register keyword
【cocos creator】点击按钮切换界面