package com.example.demo.util;
import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class ZeroCopy {
public static void main(String[] args) throws IOException {
File file = new File("D:\\tmp\\test.txt");
// mmap方式,映射为2k,生成的文件也是2k
final RandomAccessFile rw = new RandomAccessFile(file,"rw");
final MappedByteBuffer map = rw.getChannel().map(FileChannel.MapMode.READ_WRITE,0,2048);
map.put("mmap content".getBytes());
rw.close();
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel inChannel = fileInputStream.getChannel();
FileOutputStream outputStream = new FileOutputStream("D:\\tmp\\test_copy.txt");
FileChannel outChannel = outputStream.getChannel();
inChannel.transferTo(0,file.length(),outChannel);
}
}
java中使用零拷贝完成文件的写入及复制
最新推荐文章于 2024-07-04 09:24:51 发布