//1.创建两个通道
FileChannel readChannel = FileChannel.open(Paths.get("d:\\1.wmv"), StandardOpenOption.READ);
FileChannel writeChannel = FileChannel.open(Paths.get("d:\\copy02.wmv"), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND);
//2.直接内存映射文件(可以分两个来写)
// MappedByteBuffer map1 = readChannel.map(FileChannel.MapMode.READ_ONLY, 0, 1024 * 1024 * 50);
// //3.内存映射文件写入文件
// writeChannel.write(map1);
// MappedByteBuffer map2 = readChannel.map(FileChannel.MapMode.READ_ONLY, 1024 * 1024 * 50, (readChannel.size() - (1024 * 1024 * 50)));
// writeChannel.write(map2);
MappedByteBuffer map = readChannel.map(FileChannel.MapMode.READ_ONLY, 0, readChannel.size());
writeChannel.write(map);
//4.关闭
readChannel.close();
writeChannel.close();
System.out.println("复制完毕");
NIO内存映射文件的用法
最新推荐文章于 2023-09-12 19:21:39 发布