当前位置:网站首页>Use filechannel to copy files
Use filechannel to copy files
2022-07-03 07:59:00 【Yanchi】
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();
) {
// The maximum transmission at one time 2g
from.transferTo(0,from.size(),to);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Because the above method can only transmit 2G The file of , The following code optimizes the above code !!
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();
) {
// Bytes of source file
long size = from.size();
// left Represents how many bytes are left
for (long left = size; left > 0;){
left -= from.transferTo((size - left), left, to);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
边栏推荐
猜你喜欢

什么是数据类型?数据类型有什么用?

Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation

I want to do large screen data visualization application feature analysis

*p++、*++p、++*p、(*p)++

haproxy+keepalived搭建01

超限黑客认知

WorldView卫星遥感影像数据/米级分辨率遥感影像

Pat grade a 1027 colors in Mars

STM32F103 SPI (pit Diary)

Lua framwrok framework starts
随机推荐
Idea dereference display effect
PHP微信抢红包的算法
PostGIS space function
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
Research shows that breast cancer cells are more likely to enter the blood when patients sleep
Pat class a 1030 travel plan
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)
How does yarn link help developers debug NPM packages?
Introduction of novel RNA based cancer therapies
[at] ABC 258g - triple Reach - violence
Client server model
oracle 插入单引号
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
Getting started with minicom
the installer has encountered an unexpected error installing this package
超限黑客认知
Differences between tp3.2 and tp5.0
华为交换机基础配置(telnet/ssh登录)