Use Channel in the NIO

The main channel implementation class

import java.nio.channels.DatagramChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

 

Acquisition channel way

  1. JAVA provides support for the passage of getChannel class () method
    local IO:
          
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.RandomAccessFile;

    Network IO:

    import java.net.ServerSocket;
    import java.net.Socket;
    import java.nio.channels.DatagramChannel;
  2. jdk1.7 after NIO.2 provides a static method open for each channel ()
  3. Files in the NIO.w jdk1.7 tool or the like newByteChannel ()

Copy the files have finished using the channel: a Case

  1. Non-direct buffers:
    import org.junit.jupiter.api.Test;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    
    public class ChannelTest {
    
        @Test
        public void channel() throws IOException {
    
            FileInputStream fis=new FileInputStream("文件1的路径");
            FileOutputStream fos=new FileOutputStream("文件2的路径");
    
            //1.获取通道
            FileChannel inChannel=fis.getChannel();
            FileChannel outChannel=fos.getChannel();
    
            //2.分配指定大小的缓冲区
            ByteBuffer buffer=ByteBuffer.allocate(1024);
    
            //3.将通道中的数据存入缓冲区中
            while (inChannel.read(buffer)!=-1){
                buffer.flip();//切换读取数据的模式
                outChannel.write(buffer);//写入数据
                buffer.clear();//清空缓冲去
            }
            //关闭流和通道
            inChannel.close();
            outChannel.close();
            fis.close();
            fos.close();
        }
    }
    
  2. Direct buffers
    import org.junit.jupiter.api.Test;
    
    import java.io.IOException;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.file.Paths;
    import java.nio.file.StandardOpenOption;
    
    
    public class ChannelTest {
        @Test
        public void channel2() throws IOException {
            FileChannel inchannel=FileChannel.open(Paths.get("文件1的路径"), StandardOpenOption.READ);
            //CREATE_NEW  如果存在则报错,如果不存在则报错。READ只读模式。WRITE存在则覆盖,不存在则创建
            FileChannel outchannel=FileChannel.open(Paths.get("文件2的路径"),StandardOpenOption.WRITE,StandardOpenOption.CREATE_NEW);
    
            //内存映射文件
            MappedByteBuffer inMappedBuf=inchannel.map(FileChannel.MapMode.READ_ONLY,0,inchannel.size());
            MappedByteBuffer outMappedBuf=inchannel.map(FileChannel.MapMode.READ_WRITE,0,inchannel.size());
    
            //对缓冲区进行数据的读写操作
            byte[] dst=new byte[inMappedBuf.limit()];
            inMappedBuf.get(dst);
            outMappedBuf.put(dst);
    
            inchannel.close();
            outchannel.close();
    
        }
    }

    The easiest way:
    transferTo transport
    (problems between the difference between the two is just back and forth) transferFrom transmission

    import org.junit.jupiter.api.Test;
    
    import java.io.IOException;
    import java.nio.channels.FileChannel;
    import java.nio.file.Paths;
    import java.nio.file.StandardOpenOption;
    
    
    public class ChannelTest {
    
        @Test
        public void channel3() throws IOException {
            //读取文件
            FileChannel inchannel=FileChannel.open(Paths.get("文件1的位置"),StandardOpenOption.READ);
            //目标文件的位置
            FileChannel outchannel=FileChannel.open(Paths.get("文件2的位置"),StandardOpenOption.CREATE_NEW);
    
            inchannel.transferTo(0,inchannel.size(),outchannel);
            
            inchannel.close();
            outchannel.close();
    
        }
    }

     

 

 

Published 242 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41813208/article/details/103837242