首页 > 移动平台 > 详细

Java NIO 利用通道完成文件复制(MappedByteBuffer)

时间:2018-09-11 11:58:52      阅读:229      评论:0      收藏:0      [点我收藏+]
相关学习网址:

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import org.junit.Test;

@Test
public void test2() throws IOException{
long start = System.currentTimeMillis();

    FileChannel inChannel = FileChannel.open(Paths.get("1.mp4"), StandardOpenOption.READ);
    FileChannel outChannel = FileChannel.open(Paths.get("2.mp4"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);

    //内存映射文件
    MappedByteBuffer inMappedBuf = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
    MappedByteBuffer outMappedBuf = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());

    //直接对缓冲区进行数据的读写操作
    byte[] dst = new byte[inMappedBuf.limit()];
    inMappedBuf.get(dst);
    outMappedBuf.put(dst);

    inChannel.close();
    outChannel.close();

    long end = System.currentTimeMillis();
    System.out.println("耗费时间为:" + (end - start));
}

Java NIO 利用通道完成文件复制(MappedByteBuffer)

原文:http://blog.51cto.com/11056727/2173580

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!