5、NIO--通道的数据传输

实例1:

利用通完完成文件的复制(非直接缓冲区)

@Test
    public void test4() throws IOException{
                FileInputStream fis = new FileInputStream("d:\a.jpg");
        FileOutputStream fos = new FileOutputStream("d:\b.jpg");
        
        //获取通道
        FileChannel inChannel=fis.getChannel();
        FileChannel outChannel = fos.getChannel();
        
        //指定大小的缓冲区
        ByteBuffer buf = ByteBuffer.allocate(2048);
        
        //将通道中的数据存入缓冲区
        while(inChannel.read(buf) != -1){
            //切换成读取数据的模式
            buf.flip();
            
            //将缓冲区的数据写入通道
            outChannel.write(buf);
            
            //清空缓冲区
            buf.clear();
            
        }
        
    //关闭 outChannel.close(); inChannel.close(); fos.close(); fis.close(); }

实例2:

使用直接缓冲区完成文件的复制(内存映射文件的方式)

等同于allocateDirect()方法

@Test
    public void test5() throws IOException{
        
        //open(Path path, OpenOption... options) 
        //读取
        FileChannel inChannel = FileChannel.open(Paths.get("d:\a.jpg"), StandardOpenOption.READ);
        //写入
        //StandardOpenOption.CREATE:存在就创建,不存在也进行创建(覆盖)
        //StandardOpenOption.CREATE_NEW:存在就报错,不存在就创建
     //此时需要三个权限 读 写 创建
FileChannel outChannel = FileChannel.open(Paths.get("d:\c.jpg"),
          StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);
//map(MapMode mode, long position, long size) //分别代表操作方式,操作的位置,以及大小 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(); }

通道之间的数据传输

--transferForm():将数据从源通道传输到其他 Channel 中

(ReadableByteChannel src, long position, long count)
src文件从positoion位置到count位置的数据获取
 

--transferTo():将数据从源通道传输到其他 Channel 中

transferTo(long position, long count, WritableByteChannel target)

文件从positioncount位置发送到target

注意:一下两种方法都是直接缓存区

    public void test6() throws IOException{
        
        FileChannel inChannel = FileChannel.open(Paths.get("d:\a.jpg"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("d:\d.jpg"), 
        StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE); inChannel.transferTo(
0, inChannel.size(), outChannel); //关闭 inChannel.close(); outChannel.close(); }

文件从inChannel中来发送到outChannel

	@Test
	public void test6() throws IOException{
		
		FileChannel inChannel = FileChannel.open(Paths.get("d:\a.jpg"), 
              StandardOpenOption.READ); FileChannel outChannel = FileChannel.open(Paths.get("d:\e.jpg"),
            StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE); //inChannel.transferTo(0, inChannel.size(), outChannel); outChannel.transferFrom(inChannel, 0, inChannel.size()); //关闭 inChannel.close(); outChannel.close(); }

原文地址:https://www.cnblogs.com/Mrchengs/p/10822963.html