当前位置:网站首页>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();
}
}
}
边栏推荐
- Huawei switches are configured with SSH login remote management switches
- JS regular case-
- haproxy+keepalived集群搭建02
- Pat grade a 1029 median
- WorldView卫星遥感影像数据/米级分辨率遥感影像
- Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
- [MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
- 一篇文章让你读懂-曼彻斯特编码
- My touch screen production "brief history" 1
- LwIP learning socket (application)
猜你喜欢

【LeetCode】2. Valid parentheses · valid parentheses

I want to do large screen data visualization application feature analysis

Wechat applet taro learning record

Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“

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

How to configure GDAL under idea

【LeetCode】3. Merge two sorted lists · merge two ordered linked lists

Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework

Free use until 2015 -- viz artist multi touch plug-in package

Pat class a 1032 sharing
随机推荐
Oracle queries grouped by time
I want to do large screen data visualization application feature analysis
[step on the pit series] MySQL failed to modify the root password
Clip Related Script
Technical dry goods | some thoughts on the future of AI architecture
Ilruntime learning - start from scratch
Lua framwrok framework starts
链式长取值
Enter three times and guess a number
【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
IP production stream is so close to me
WorldView卫星遥感影像数据/米级分辨率遥感影像
Pat grade a 1029 median
WPF:解决MaterialDesign:DialogHost 无法关闭问题
使用 FileChannel 进行文件的复制拷贝
PIP uses image website to solve the problem of slow network speed
Luaframwrok handles resource updates
Go language - loop statement
Microsoft Security Response Center
the installer has encountered an unexpected error installing this package