package org.demo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Demo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream fis=new FileInputStream("D:\\cc.MP4");
FileOutputStream fos=new FileOutputStream("D:\\Copycc.MP4");
FileChannel ic=fis.getChannel();
FileChannel oc=fos.getChannel();
ByteBuffer bf=ByteBuffer.allocateDirect(52428800);
while (true) {
bf.clear();
int num=ic.read(bf);
if (num<0) {
break;
}
bf.flip();
oc.write(bf);
}
System.out.println("下载成功");
}
}
NIO使用50MB操作5G文件
最新推荐文章于 2026-06-30 09:24:45 发布
这是一个使用Java NIO实现的文件复制程序,从'D:cc.MP4'读取内容并写入'D:Copycc.MP4'。程序通过FileInputStream和FileOutputStream以及FileChannel进行操作,利用ByteBuffer缓存数据,实现了大文件的高效复制。
356

被折叠的 条评论
为什么被折叠?



