当前位置:网站首页>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();
}
}
}
边栏推荐
- Zohocrm deluge function application time verification
- 【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
- Client server model
- Redis batch startup and shutdown script
- 【LeetCode】4. Best time to buy and sell stock
- Idea dereference display effect
- 【踩坑系列】mysql 修改root密码失败
- JSON与Object之间转换
- Ventuz Foundation Series "one step at the door"
- Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
猜你喜欢

一条通往服务器所有端口的隧道

Zohocrm deluge function application time verification

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

My touch screen production "brief history" 2

Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire
![[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December](/img/51/81ceaf8746ec7455ea8abf9f038e81.jpg)
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December

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

Viz artist advanced script video tutorial -- stringmap use and vertex operation

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

haproxy+keepalived搭建01
随机推荐
How to clear the console password for s7700 device
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
Unity2019_ Natural ambient light_ Sky box
Zohocrm deluge function application time verification
【LeetCode】2. Valid parentheses · valid parentheses
微软安全响应中心
Luaframwrok handles resource updates
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
An article for you to understand - Manchester code
An intern's journey to cnosdb
Client server model
Worldview satellite remote sensing image data / meter resolution remote sensing image
Ventuz Foundation Series "one step at the door"
华为交换机配置ssh登录远程管理交换机
What to do after the browser enters the URL
Lua hot update basic grammar
Redis配置文件
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
多旅行商问题——公式和求解过程概述
P2622 关灯问题II(状态压缩 搜索)