package com.zzj.nio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileChannelTest {
public static void main(String[] args) throws IOException {
String name = FileChannelTest.class.getResource("/video.avi").getPath();
FileInputStream inStream = new FileInputStream(name);
FileOutputStream outStream = new FileOutputStream(name + ".bak");
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (inChannel.read(byteBuffer) > -1) {
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.clear();
}
inChannel.close();
outChannel.close();
System.out.println("end!");
}
}
【Java IO】NIO复制文件
最新推荐文章于 2023-08-14 16:57:30 发布