当前位置:网站首页>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();
}
}
}
边栏推荐
猜你喜欢

Pat class a 1030 travel plan

【踩坑系列】mysql 修改root密码失败

Install cross compiler arm none liunx gnueabihf

How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03

JS common basic case sorting (continuous update)

创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03

Technical dry goods Shengsi mindspire dynamic transformer with variable sequence length has been released!

Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico

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

Getting started with minicom
随机推荐
oracle中的 (+)是什么意思
Unity2019_ Natural ambient light_ Sky box
华为交换机:配置telnet和ssh、web访问
Technical dry goods | some thoughts on the future of AI architecture
Structure of golang
Pat grade a 1029 median
Usage of (case, when) in PostgreSQL
What to do after the browser enters the URL
Lua hot update basic grammar
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
IP production stream is so close to me
LwIP learning socket (application)
微软安全响应中心
the installer has encountered an unexpected error installing this package
Pat class a 1028 list sorting
Getting started with minicom
Ilruntime learning - start from scratch
什么是定义?什么是声明?它们有何区别?
Clip Related Script
Static keyword