NIO中的Channel的使用

通道的主要实现类

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

获取通道的方式

  1. JAVA针对支持通道的类提供了getChannel()方法
    本地IO:
          
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.RandomAccessFile;

    网络IO:

    import java.net.ServerSocket;
    import java.net.Socket;
    import java.nio.channels.DatagramChannel;
  2. jdk1.7以后NIO.2 针对各个通道提供了静态方法open()
  3. jdk1.7中NIO.w的Files工具类的newByteChannel()

案例一:使用 通道完成文件的复制

  1. 非直接缓冲区:
    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. 直接缓冲区
    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();
    
        }
    }

    最简单的方式:
    transferTo传输
    transferFrom传输(两者的区别只是来回之间的问题)

    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();
    
        }
    }
发布了242 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41813208/article/details/103837242