当前位置:网站首页>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();
}
}
}
边栏推荐
- MaxCompute字符串分割函数-SPLIT_PART
- tp3.2和tp5.0的区别
- 华为交换机配置ssh登录远程管理交换机
- Quality blog——
- *p++、*++p、++*p、(*p)++
- Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)
- Wechat native applet cloud development learning record 01
- Zohocrm deluge function application time verification
- Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
- Screenshot tool snipaste
猜你喜欢
Harmonyos third training notes
The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
Pat class a 1030 travel plan
the installer has encountered an unexpected error installing this package
EtherCAT state machine transition (ESM)
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
IP production stream is so close to me
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
[untitled]
Zohocrm deluge function application time verification
随机推荐
一个实习生的CnosDB之旅
【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
How to configure GDAL under idea
PHP wechat red packet grabbing algorithm
idea取消引用顯示效果
tp3.2和tp5.0的区别
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
Redis batch startup and shutdown script
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
Huawei switches are configured with SSH login remote management switches
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
Lua framwrok framework starts
Zohocrm deluge function application time verification
the installer has encountered an unexpected error installing this package
What to do after the browser enters the URL
使用 FileChannel 进行文件的复制拷贝
Free use until 2015 -- viz artist multi touch plug-in package
static关键字
Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)