当前位置:网站首页>使用 FileChannel 进行文件的复制拷贝
使用 FileChannel 进行文件的复制拷贝
2022-07-03 07:53:00 【雁迟】
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();
) {
// 一次最高只能传输 2g
from.transferTo(0,from.size(),to);
} catch (IOException e) {
e.printStackTrace();
}
}
}
由于以上方法最大只能传输 2G 的文件,下面的代码对以上代码进行优化!!
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();
) {
// 源文件字节数
long size = from.size();
// left 代表还剩下多少字节
for (long left = size; left > 0;){
left -= from.transferTo((size - left), left, to);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
边栏推荐
- [MySQL 12] MySQL 8.0.18 reinitialization
- idea取消引用显示效果
- 什麼是定義?什麼是聲明?它們有何區別?
- 什么是数据类型?数据类型有什么用?
- 【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
- Oracle queries grouped by time
- JS to implement publish and subscribe
- Quelle est la définition? Qu'est - ce qu'une déclaration? Quelle est la différence?
- Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward
- Register keyword
猜你喜欢
【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
OSPF experiment
PAT甲级 1031 Hello World for U
JS common basic case sorting (continuous update)
Go language foundation ----- 02 ----- basic data types and operators
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
STM32F103 SPI (pit Diary)
Go language foundation ----- 15 ----- reflection
Technical dry goods | some thoughts on the future of AI architecture
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
随机推荐
PAT甲级 1031 Hello World for U
华为交换机配置ssh登录远程管理交换机
Wechat native applet cloud development learning record 01
PHP common sorting algorithm
Huawei switch console password reset, device initialization, default password
密西根大学张阳教授受聘中国上海交通大学客座教授(图)
Go language foundation ----- 19 ----- context usage principle, interface, derived context (the multiplexing of select can be better understood here)
Install cross compiler arm none liunx gnueabihf
HDMI2.1与HDMI2.0的区别以及转换PD信号。
Static keyword
E: 无法定位软件包 ros-melodic-desktop-full
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
Enter three times and guess a number
【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表
Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once
PAT甲级 1027 Colors in Mars
Redis配置文件
*p++、*++p、++*p、(*p)++
Uniapp learning records
Docker installs MySQL and successfully uses Navicat connection