最快复制文件方法() out.transferFrom(in, 0, in.size());

本文提供了一个简单的Java程序,展示了如何使用Java NIO API进行文件复制操作,包括打开输入和输出文件流,获取文件通道,并利用transferFrom方法进行高效的数据传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 package test;
 2 import java.io.*;
 3 import java.nio.channels.FileChannel;
 4 public class Test31
 5 {
 6     public static void main(String[] args) throws Exception
 7     {
 8         String dir = "E:/";
 9         // 调用方法
10         copyFile(dir + "DV-1676.mp4", dir + "DV-1676_copy.mp4");
11     }
12     public static boolean copyFile(String readfile, String writefile) throws Exception
13     {
14         FileInputStream fis = null;
15         FileOutputStream fos = null;
16         // 定义两个直连的文件管道
17         FileChannel in = null, out = null;
18         fis = new FileInputStream(readfile);
19         fos = new FileOutputStream(writefile);
20         in = fis.getChannel();
21         out = fos.getChannel();
22         // 直接让流流向要拷贝的文件
23         out.transferFrom(in, 0, in.size());
24         in.close();
25         out.close();
26         fis.close();
27         fos.close();
28         return true;
29     }
30 }

 

转载于:https://www.cnblogs.com/stone1022/p/4051762.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值