zoukankan      html  css  js  c++  java
  • NIO

    1 Java NIO简介

    Java NIO是从Java1.4版本开始引入的一个新的IO的API,可以替代标准的Java的IO的API。NIO和原来的IO有同样的作用和目的,但是使用的方式完全不同,NIO支持面向缓冲区的、给予通道的IO操作。NIO将以更加高效的方式进行文件的读写操作。

    2 Java NIO和IO的主要区别

    IO NIO
    面向流(Stream Oriented) 面向缓冲区(Buffer Oriented)
    阻塞IO(Blocking IO) 非阻塞IO(Non Blocking IO)
    选择器(Selectors)

    3 缓冲区(Buffer)和通道(Channel)

    Java NIO系统的核心在于:通道(Channel)和缓冲区(Buffer)。通道表示打开到IO设备(例如:文件、Socket)的连接。如果需要使用NIO系统,需要获取用于连接IO设备的通道以及用于容纳数据的缓冲区,然后操作缓冲区,对数据进行处理。

    • 简而言之,Channel负责传输,Buffer负责存储。

    3.1 缓冲区(Buffer)

    3.1.1 概述

    • 缓冲区(Buffer):一个用于特定基本数据类型的容器。由java.nio包定义的,所有缓冲区都是Buffer抽象类的子类。
    • Java NIO中的Buffer主要用于和NIO通道进行交互,数据是从通道读入到缓冲区的,然后从缓冲区中写入到通道中的。
    • Buffer就像一个数组,可以保存多个相同类型的数据。根据数据类型的不同(boolean)除外,有以下Buffer常用子类:ByteBuffer、CharBuffer、ShortBuffer、IntBuffer、LongBuffer、FloatBuffer、DoubleBuffer。上述Buffer类他们都是此阿勇相似的方法进行管理数据的,只是各自管理的数据类型不同而已。都是通过如下的方法获取一个Buffer对象:
    public static XxxBuffer allocate(int capacity) {}
    

    3.1.2 缓冲区的基本属性

    • 容量(capacity):表示Buffer最大数据容量,缓冲区容量不能为负,并且一旦创建不能更改。

    • 限制(limit):第一个不应该读取或写入的数据的索引,即位于limit后的数据不可读写。缓冲区的限制不能为负,并且不能大于容量。

    • 位置(position):下一个要读取或写入的数据的索引。缓冲区的位置不能为负,并且不能大于其限制。

    • 标记(mark)和重置(reset):标记是一个索引,通过Buffer中的mark()方法指定Buffer中的一个特定的position,之后可以通过调用reset()方法恢复到这个position。

    • 简而言之:0 <= mark <= position <= limit <= capacity。

    3.1.3 Buffer的常用方法

    方法 说明
    Buffer clear() 清空缓冲区并返回对缓冲区的引用
    Buffer flip() 将缓冲区的界限设置为当前位置,并将当前位置设置为0。切换到读数据模式。
    int Capacity() 返回Buffer的capacity大小
    boolean hasRemaining() 判断缓冲区中是否还有元素
    int limit() 返回Buffer的界限(limit)的位置
    Buffer limit(int n) 设置缓冲区界限为n,并返回一个具有新limit的缓冲区对象
    Buffer mark() 对缓冲区进行标记
    int position() 返回缓冲区的当前位置position
    Buffer position(int n) 将设置缓冲区的当前位置为n,并返回修改后的Buffer对象
    int remaining() 返回position和limit之间的元素个数
    Buffer reset() 将位置position为以前设置的mark所在的位置
    Buffer rewind() 将位置position设置为0,取消设置的mark

    3.1.4 缓冲区的数据操作

    • Buffer所有子类提供了两个数据操作的方法:get()和put()方法。

    3.1.5 Buffer的应用示例

    • 示例:
    package com.weiwei.xu;
    
    import org.junit.Test;
    
    import java.nio.ByteBuffer;
    
    /**
     * 一、缓冲区(Buffer):在Java NIO中负责数据的存取。缓冲区就是数组。数组是用于存储相同数据类型的数据。
     * 根据数据类型的不同,提供了相应类型的缓冲区(boolean除外):ByteBuffer、CharBuffer、ShortBuffer、IntBuffer、LongBuffer、FloatBuffer、DoubleBuffer。
     * 上述缓冲区的管理方式几乎一致,都是通过allocate()获取缓冲区。
     *
     * 二、缓冲区存取数据的两个核心方法:
     * put():存入数据到缓冲区。
     * get():、。
     *
     * 三、缓冲区中的四个核心属性。
     * capacity:容量,表示缓冲区中最大存储数据的容量。一旦声明,不能更改。
     * limit:限制,界限,表示缓冲区可以操作数据的大小。(limit后面的数据不能进行读写)。
     * position:位置,表示缓冲区中正在操作数据的位置。
     * mark:标记,表示记录当前position的位置,可以通过reset()恢复到mark的位置。
     *
     * 0 <= mark <= position <= limit <= capacity
     */
    public class BufferTest {
    
        @Test
        public void test1() {
            //分配一个指定大小的缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
    
            System.out.println("----------------allocate--------------");
            System.out.println("position:" + buffer.position());//0
            System.out.println("limit:" + buffer.limit());//1024
            System.out.println("capacity:" + buffer.capacity());//1024
    
            //利用put()方法存入数据到缓冲区中
            String str = "abcde";
            buffer.put(str.getBytes());
    
            System.out.println("----------------put--------------");
            System.out.println("position:" + buffer.position());//5
            System.out.println("limit:" + buffer.limit());//1024
            System.out.println("capacity:" + buffer.capacity());//1024
    
            //切换到读数据模式
            buffer.flip();
            System.out.println("----------------flip--------------");
            System.out.println("position:" + buffer.position());//0
            System.out.println("limit:" + buffer.limit());//5
            System.out.println("capacity:" + buffer.capacity());//1024
    
            //利用get()方法读取数据
            byte[] dst = new byte[buffer.limit()];
            buffer.get(dst);
    
            System.out.println("获取缓冲区中的数据:" + new String(dst, 0, dst.length));
            System.out.println("----------------get--------------");
            System.out.println("position:" + buffer.position());//5
            System.out.println("limit:" + buffer.limit());//5
            System.out.println("capacity:" + buffer.capacity());//1024
    
            //重复读数据,将位置position设置为0
            buffer.rewind();
            System.out.println("----------------rewind--------------");
            System.out.println("position:" + buffer.position());//0
            System.out.println("limit:" + buffer.limit());//5
            System.out.println("capacity:" + buffer.capacity());//1024
    
            //清空缓冲区,但是缓冲区的数据依然存在,数据处于“被遗忘状态”。
            buffer.clear();
            System.out.println("----------------clear--------------");
            System.out.println("position:" + buffer.position());//0
            System.out.println("limit:" + buffer.limit());//1024
            System.out.println("capacity:" + buffer.capacity());//1024
        }
    
        @Test
        public void test2() {
            ByteBuffer buffer = ByteBuffer.allocate(1024);
    
            String str = "abcde";
            buffer.put(str.getBytes());
    
            buffer.flip();
    
            byte[] dst = new byte[buffer.limit()];
            buffer.get(dst,0,2);
            System.out.println(new String(dst,0,2));
    
            System.out.println("position:"+buffer.position());//2
    
            buffer.mark();
    
            buffer.get(dst,2,2);
    
            System.out.println(new String(dst,2,2));
    
            System.out.println("position:"+buffer.position());//4
    
            buffer.reset();
    
            System.out.println("position:"+buffer.position());//2
    
            //判断缓冲区中是否含有剩余数据
            if(buffer.hasRemaining()){
                System.out.println("获取缓冲区中可以操作的数量"+buffer.remaining());
            }
    
        }
    }
    
    

    缓冲区的基本属性

    3.1.6 直接缓冲区和非直接缓冲区

    • 字节缓冲区要么是直接的,要么是非直接的。如果为直接字节缓冲区,则Java虚拟机会尽最大努力直接在此缓冲区上执行本机IO操作。也就是说,在每次调用基础操作系统的一个本机I/O操作之前(或之后),虚拟机都会尽量避免将缓冲区的内容复制到中间缓冲区中(或从中间缓冲区中复制内容)。
    • 直接字节缓冲区可以通过调用此类的allocateDirect()工厂方法来创建。此方法返回的缓冲区进行分配和取消分配所需成本通常要高于非直接缓冲区。直接缓冲区的内容可以驻留在常规的垃圾回收堆之外,因此,它们对应用程序的内存需求量造成的影响可能并不明显。所以,建议将直接缓冲区主要分配给那些易受基础系统的本地IO操作相应的大型、持久的缓冲区。一般情况下,最好仅在直接缓冲区能在程序性能方面带来明显好处时分配它们。
    • 直接字节缓冲区还可以通过FileChannel的map()方法将文件区域直接映射到内存中来创建。该方法返回MappedByteBuffer。Java平台的实现有助于通过JNI从本机代码创建直接字节缓冲区。如果以上这些缓冲区中的某个缓冲区实例指的是不可访问的内存区域,则试图访问该区域不会更改该缓冲区的内容,并且将会在访问期间或稍后的某个时间导致抛出不确定的异常。
    • 字节缓冲区是直接缓冲区还是非直接缓冲区可以通过isDirect()方法来确定。提供此方法是为了能够在性能关键型代码中执行显示缓冲区管理。

    非直接缓冲区

    直接缓冲区

    3.2 通道(Channel)

    3.2.1 概述

    • 通道(Channel):由java.nio.channels包定义的。Channel表示IO源和目标打开的链接。Channel类似于传统的“流”。只不过CHannel本身不能直接访问数据,Channel只能和Buffer进行交互。

    通道概述

    3.2.2 通道的主要实体类

    • Java为Channel(java.nio.channels.Channel)接口提供的最主要的实现类如下:
      • FileChannel:用于读取、写入、映射和操作文件的通道。
      • DatagramChannel:通过UDP读写网络中的数据通道。
      • SocketChannel:通过TCP读写网络中的数据。
      • ServerSocketChannel:可以监听新进来的TCP连接,对每一个新进来的连接都会创建一个SocketChannel。

    3.2.3 获取通道

    • ①Java针对支持通道的类提供了getChannel()方法
      • FileInputStream
      • FileOutputStream
      • RandomAccessFile
      • Socket
      • ServerSocket
      • DatagramSocket
    • ②JDK1.7中的NIO2提供了针对各个通道提供了静态方法open()方法。
    • ③JDK1.7中的NIO2的Files工具类的newByteChannel()方法。

    3.2.4 应用示例

    package com.weiwei.xu;
    
    import org.junit.Test;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.file.Paths;
    import java.nio.file.StandardOpenOption;
    
    /**
     * 一、通道(Channel):用于源节点和目标节点之间的连接。在Java NIO中负责缓冲区中数据的传输。Channel本身是不存储数据,因此需要配合缓冲区进行操作。
     * 二、通道的主要实现类
     * java.nio.channels.Channel接口
     * |--FileChannel
     * |--SocketChannel
     * |--ServerSocketChannel
     * |--DatagramChannel
     * 三、获取通道
     * ①Java支持通道的类提供了getChannel()方法。
     * 本地IO:FileInputStream、FileOutputStream、RandomAccessFile
     * 网络IO:Socket、ServerSocket、DatagramSocket
     * ②在JDK1.7中的NIO2针对各个通道提供了静态方法open()。
     * ③在JDK1.7中的NIO2的Files工具类的newByteChannel()。
     * 四、通道之间的数据传输
     * transferFrom()
     * transferTo()
     */
    public class ChannelTest {
    
        /**
         * 利用直接缓冲区完成文件的复制
         *
         * @throws IOException
         */
        @Test
        public void test3() throws IOException {
            FileChannel inFileChannel = FileChannel.open(Paths.get("D:\project\nio\1.jpg"), StandardOpenOption.READ);
            FileChannel outFileChannel = FileChannel.open(Paths.get("D:\project\nio\2.jpg"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
    
            inFileChannel.transferTo(0, inFileChannel.size(), outFileChannel);
    
            outFileChannel.close();
            inFileChannel.close();
        }
    
    
        /**
         * 利用直接缓冲区完成文件的复制(内存映射文件)
         */
        @Test
        public void test2() throws IOException {
            FileChannel inFileChannel = FileChannel.open(Paths.get("D:\project\nio\1.jpg"), StandardOpenOption.READ);
            FileChannel outFileChannel = FileChannel.open(Paths.get("D:\project\nio\2.jpg"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
    
            //内存映射文件
            MappedByteBuffer inMappedByteBuffer = inFileChannel.map(FileChannel.MapMode.READ_ONLY, 0, inFileChannel.size());
            MappedByteBuffer outMappedByteBuffer = outFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, inFileChannel.size());
    
            //直接对缓冲区进行数据的读写操作
            byte[] dst = new byte[inMappedByteBuffer.limit()];
            inMappedByteBuffer.get(dst);
    
            outMappedByteBuffer.put(dst);
    
            inFileChannel.close();
            outFileChannel.close();
        }
    
    
        /**
         * 利用通道完成文件的复制(非直接缓冲区)
         *
         * @throws IOException
         */
        @Test
        public void test() throws IOException {
            FileInputStream fileInputStream = new FileInputStream("D:\project\nio\1.jpg");
            FileOutputStream fileOutputStream = new FileOutputStream("D:\project\nio\2.jpg");
    
            //①获取通道
            FileChannel fileInputStreamChannel = fileInputStream.getChannel();
            FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
    
            //②分配指定大小的缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
    
            //③将通道中的数据存入缓冲区
            while (-1 != fileInputStreamChannel.read(buffer)) {
                buffer.flip();
                //④将缓冲区的数据写入通道
                fileOutputStreamChannel.write(buffer);
                //清空缓冲区
                buffer.clear();
            }
    
            fileOutputStreamChannel.close();
            fileInputStreamChannel.close();
            fileOutputStream.close();
            fileInputStream.close();
    
    
        }
    }
    

    3.3 文件通道(FileChannel)

    3.3.1 FileChannel常用方法

    方法 描述
    int read(ByteBuffer dst) 从Channel中读取数据到ByteBuffer
    long read(ByteBuffer[] dsts) 将Channel中的数据“分散”到ByteBuffer[]
    int write(ByteBuffer src) 将ByteBuffer中的数据写入到Channel
    long write(ByteBuffer[] srcs) 将ByteBuffer[]中的数据“聚集”到Channel
    long position() 返回此通道的文件位置
    FileChannel position(long p) 设置此通道的文件位置
    long size() 返回此通道的文件的当前大小
    FileChannel truncate(long s) 将此通道的文件截取到给定大小
    void force(boolean metaData) 强制将所有对此通道的文件更新写入到存储设备中

    3.3.2 应用示例

    /**
     * 利用通道完成文件的复制(非直接缓冲区)
     *
     * @throws IOException
     */
    @Test
    public void test() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("D:\project\nio\1.jpg");
        FileOutputStream fileOutputStream = new FileOutputStream("D:\project\nio\2.jpg");
    
        //①获取通道
        FileChannel fileInputStreamChannel = fileInputStream.getChannel();
        FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
    
        //②分配指定大小的缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);
    
        //③将通道中的数据存入缓冲区
        while (-1 != fileInputStreamChannel.read(buffer)) {
            buffer.flip();
            //④将缓冲区的数据写入通道
            fileOutputStreamChannel.write(buffer);
            buffer.clear();
        }
    
        fileOutputStreamChannel.close();
        fileInputStreamChannel.close();
        fileOutputStream.close();
        fileInputStream.close();
    }
    

    3.4 分散(Scatter)和聚集(Gather)

    3.4.1 概述

    • 分散读取(Scattering Reads)是指从Channel中读取的数据“分散”到多个Buffer中。

    分散读取

    注意:按照缓冲区的顺序,从Channel中读取的数据依次将Buffer填满。

    • 聚集写入(Gathering Writes)是指将多个Buffer中的数据“聚集”到Channel。

    聚集写入

    注意:按照缓冲区的顺序,写入position和limit之间的数据到Channel。

    3.4.2 应用示例

    package com.weiwei.xu;
    
    import org.junit.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 test() throws IOException {
    
            FileInputStream fileInputStream = new FileInputStream("D:\project\nio\1.jpg");
            FileOutputStream fileOutputStream = new FileOutputStream("D:\project\nio\2.jpg");
    
            FileChannel fileInputStreamChannel = fileInputStream.getChannel();
            FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
    
    
            ByteBuffer buffer1 = ByteBuffer.allocate(512);
            ByteBuffer buffer2 = ByteBuffer.allocate(512);
    
            ByteBuffer[] buffers = new ByteBuffer[]{buffer1, buffer2};
    
            while (-1 != fileInputStreamChannel.read(buffers)) {
                for (ByteBuffer buffer : buffers) {
                    buffer.flip();
                }
                fileOutputStreamChannel.write(buffers);
                for (ByteBuffer buffer : buffers) {
                    buffer.clear();
                }
            }
    
                  
    
            fileInputStreamChannel.close();
            fileOutputStreamChannel.close();
    
        }
    }
    
    

    4 NIO的非阻塞式网络通信

    4.1 阻塞和非阻塞

    • 传统的IO流都是阻塞的。也就是说,当一个线程调用read()后write()方法的时候,该线程就会被阻塞,直到有一些数据被读取或写入,该线程在此期间不能执行其他任务。因此,在完成网络通信进行IO操作时,由于线程会阻塞,所以服务器端必须为每个客户端都提供一个独立的线程进行处理,当服务器端需要处理大量客户端请求的时候,性能急剧下降。
    • Java NIO是非阻塞的。当线程从某个通道进行读写数据的时候,如果没有数据可用的时候,该线程可用执行其他任务。线程通常将非阻塞IO的空间时间用于在其他通道上执行IO操作,所以单独的线程可用管理多个输入和输出通道。因此,NIO可以让服务器端使用一个或有限几个线程来同时处理连接到服务器端的所有客户端。

    4.2 选择器(Selector)

    4.2.1 概述

    • 选择器(Selector)是SelectableChannel对象的多路复用器,Selector可以同时监控多个SelectableChannel的IO状况,也就是说,利用Selector可以使得一个单独的线程管理多个Channel。Selector是非阻塞IO的核心。
    • SelectableChannel的结构如下图所示:

    SelectableChannel的结构

    4.2.2 SocketChannel

    • Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道。
    • 操作步骤:
      • ①打开SocketChannel。
      • ②读写数据。
      • ③关闭SocketChannel。

    4.2.3 ServerSocketChannel

    • Java NIO中的ServerSOcketChannel是一个可以监听新进来的TCP连接的通道,就像标准IO中的ServerSocket一样。

    4.2.4 DatagramChannel

    • Java NIO中的DatagramChannel是一个能收能发的UDP的通道。
    • 操作步骤:
      • ①打开DatagramChannel。
      • ②接收/发送数据。

    4.2.5 选择器(Selector的应用)

    • 当调用register(Selector sel,int ops)将通道注册选择器的时候,选择器对通道的监听事件,需要通过第二个参数ops来指定。

    • 可以监听的事件类型(可以使用SelectionKey的四个常量来表示):

      • 读:SelectionKey.OP_READ。
      • 写:SelectionKey.OP_WRITE。
      • 连接:SelectionKey.OP_CONNECT。
      • 接收:SelectionKey.OP_ACCEPT。
    • 如果注册的时候不止监听一个事件,可以使用"|"操作符连接。

    int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;
    

    4.2.6 选择器(Selector)的常用方法

    方法 描述
    Set keys() 返回所有的SelectionKey集合。代表注册在该Selector上的Channel
    Set selectedKeys() 返回此Selector的已选择键
    int select() 监控所有注册的Channel,如果有需要处理的IO操作的时候,将对应的SelectionKey加入已选择的SelectionKey集合集合中,并返回这些Channel的数量。
    int select(long timeout) 可以设置超时时长的select()操作
    int selectNow() 执行一个立即返回的select()操作,该方法不会阻塞线程
    Selector wakeup() 使得一个还未返回的select()方法立即返回
    void close() 关闭该选择器

    4.2.7 SelectionKey概述及常用方法

    • SelectionKey表示SelectableChannel和Selector之间的注册关系。每次向选择器注册通道的时候就会选择一个事件(选择键)。
    方法 描述
    int interestOps() 获取感兴趣的事件集合
    int readyOps() 获取通道已经准备就绪的操作的集合
    SelectableChannel channel() 获取注册通道
    Selector selector() 返回选择器
    boolean isReadable() 检测Channel中读事件是否就绪
    boolean isWriteable() 检测Channel中写事件是否就绪
    boolean isConnectable() 检测Channel中连接是否就绪
    boolean isAcceptable() 检测Channel中接受是否就绪

    4.2.8 应用示例

    • Java NIO阻塞式
    package com.weiwei.xu;
    
    
    
    import org.junit.Test;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.nio.file.Paths;
    import java.nio.file.StandardOpenOption;
    
    /**
     * 一、使用NIO完成网络通信的三个核心:
     * ①通道(Channel):负责连接。
     * java.nio.channels.Channel接口:
     * 		|--SelectableChannel
     * 		|--SocketChannel
     * 		|--ServerSocketChannel
     * 		|--DatagramChannel
     * 
     * 		|--Pipe.SinkChannel
     * 		|--Pipe.SourceChannel
     * ②缓冲区(Buffer):负责数据的存取。
     * ③选择器(Selector):是SelectableChannel的多路复用器。用于监控SelectableChannel的一些IO状况。
     */
    public class BlockingNIOTest {
    
        @Test
        public void client() throws IOException {
            //获取通道
            SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(9898));
    
            FileChannel fileChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
    
            //分配指定大小的缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
    
            //读取本地文件,并发送到服务器端
            while (-1 != fileChannel.read(buffer)) {
                buffer.flip();
                socketChannel.write(buffer);
                buffer.clear();
            }
    
            //通知服务器端传送完毕
            socketChannel.shutdownOutput();
    
            int len = 0;
    
            while ((len = socketChannel.read(buffer)) != -1) {
                buffer.flip();
                System.out.println(new String(buffer.array(),0,len));
                buffer.clear();
            }
    
    
            fileChannel.close();
            socketChannel.close();
        }
    
        @Test
        public void server() throws IOException {
            //获取通道
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            //绑定端口号
            serverSocketChannel.bind(new InetSocketAddress(9898));
    
            SocketChannel socketChannel = serverSocketChannel.accept();
    
            FileChannel fileChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
    
            //分配指定大小的缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
    
            while (-1 != socketChannel.read(buffer)) {
                buffer.flip();
                fileChannel.write(buffer);
                buffer.clear();
            }
    
            buffer.put("服务器端接受成功".getBytes());
            buffer.flip();
    
            socketChannel.write(buffer);
    
    
            fileChannel.close();
            socketChannel.close();
        }
    
    
    }
    
    
    • Java NIO非阻塞式
    package com.weiwei.xu;
    
    import org.junit.Test;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Date;
    import java.util.Iterator;
    
    public class NOBlockingNIOTest {
        @Test
        public void client() throws IOException {
            //获取通道
            SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(9897));
            //切换成非阻塞模式
            socketChannel.configureBlocking(false);
            //分配指定大小的缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            //发送数据给服务器端
            buffer.put(new Date().toString().getBytes());
            buffer.flip();
            socketChannel.write(buffer);
            //关闭通道
            socketChannel.close();
        }
    
        @Test
        public void server() throws IOException {
            //获取通道
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            //切换成非阻塞模式
            serverSocketChannel.configureBlocking(false);
            //绑定端口
            serverSocketChannel.bind(new InetSocketAddress(9897));
            //获取选择器
            Selector selector = Selector.open();
            //将通道注册到选择器上,并且指定监听事件
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            //轮询式的获取选择器上已经“准备就绪”的事件
            while (selector.select() > 0) {
                //获取当前选择器中所有注册的“选择键(已注册的监听事件)”
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                //遍历
                while (iterator.hasNext()) {
                    SelectionKey selectionKey = iterator.next();
                    //判断具体是什么事件准备就绪
                    if (selectionKey.isAcceptable()) {
                        //如果是“接受就绪”,获取客户端的连接
                        SocketChannel socketChannel = serverSocketChannel.accept();
                        //切换成非阻塞模式
                        socketChannel.configureBlocking(false);
                        //将该通道注册到选择器上
                        socketChannel.register(selector, SelectionKey.OP_READ);
                    } else if (selectionKey.isReadable()) {
                        //获取当前选择器上“读就绪”状态的通道
                        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                        //读取数据
                        ByteBuffer buffer = ByteBuffer.allocate(1024);
                        int len = 0;
                        while ((len = socketChannel.read(buffer)) != -1) {
                            buffer.flip();
                            System.out.println(new String(buffer.array(), 0, len));
                            buffer.clear();
                        }
    
                    }
                    //取消选择键
                    iterator.remove();
                }
    
    
            }
    
    
        }
    
    }
    

    5 Java NIO2(Path、Paths和Files)

    5.1 NIO2概述

    • 随着JDK7的发布,Java对NIO进行了极大的扩展,增强了对文件处理和文件系统特性的支持,以至于我们称其为NIO2。因为NIO提供的一些功能,NIO已经成为文件处理中越来越重要的部分。

    5.2 Path和Paths

    • java.nio.file.Path接口代表一个平台无关的平台路径,描述了目录结构中文件的位置。
    • Paths提供的get()方法用来获取Path对象:
    //用于将多个字符串连成路径
    public static Path get(String first, String... more)
    
    • Path常用方法:
    //判断是否以path路径结束
    boolean endsWith(String path)
    
    //判断是否以path路径开始
    boolean endsWith(String path)
    
    //判断是否是绝对路径
    boolean isAbsolute();
    
    //返回和调用Path对象关联的文件名
    Path getFileName();
    
    //返回指定索引位置index的路径名称
    Path getName(int index);
    
    //返回Path根目录后面元素的数量
    int getNameCount();
    
    //返回Path对象包含整合路径,不包含Path对象指定的文件路径
    Path getParent();
    
    //返回调用Path对象的根路径
    Path getRoot();
    
    //将相对路径解析为绝对路径
    Path resolve(Path other);
    
    //作为绝对路径返回调用Path对象
    Path toAbsolutePath();
    
    //返回调用Path对象的字符串的表示形式
    String toString();
    

    应用示例:

    package com.weiwei.xu;
    
    import org.junit.Test;
    
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class NIO2Test {
    
        @Test
        public void test() {
            Path path = Paths.get("D:\project\nio\1.jpg");
    
            System.out.println(path.endsWith("1.jpg"));//true
            System.out.println(path.startsWith("D:\"));//true
    
            System.out.println(path.isAbsolute());//true
            System.out.println(path.getFileName());//1.jpg
    
            for (int i = 0; i < path.getNameCount(); i++) {
                System.out.println(path.getName(i));
            }
        }
    }
    

    5.3 Files类

    • java.nio.file.Files用于操作文件或目录的工具类。
    • Files常用方法:
    //文件复制
    public static Path copy(Path source, Path target, CopyOption... options)
    
    //创建一个目录
    public static Path createDirectory(Path dir, FileAttribute<?>... attrs)
    
    //创建一个文件
    public static Path createFile(Path path, FileAttribute<?>... attrs)
    
    //删除一个文件
    public static void delete(Path path) throws IOException
    
    //将source移动到target位置
    public static Path move(Path source, Path target, CopyOption... options)
    
    //返回Path指定文件的大小
    public static long size(Path path) throws IOException
    
    //判断文件是否存在
    public static boolean exists(Path path, LinkOption... options)
    
    //判断是否是目录
    public static boolean isDirectory(Path path, LinkOption... options)
    
    //判断是否是可执行文件
    public static boolean isExecutable(Path path)
    
    //判断是否是隐藏文件
    public static boolean isHidden(Path path) throws IOException
    
    //判断文件是否可读
    public static boolean isReadable(Path path)
    
    //判断文件是否可写
    public static boolean isWritable(Path path)
    
    //判断文件是否不存在
    public static boolean notExists(Path path, LinkOption... options)
    
    //获取和Path指定的文件相关联的属性
    public static <A extends BasicFileAttributes> A readAttributes(Path path,Class<A> type,LinkOption...options)throws IOException
    
    //获取和指定文件的连接,how指定打开方式
    public static SeekableByteChannel newByteChannel(Path path, OpenOption... options)throws IOException
    
    //打开Path指定的目录
    public static DirectoryStream<Path> newDirectoryStream(Path dir) throws IOException
    
    //获取InputStream对象
    public static InputStream newInputStream(Path path, OpenOption... options) throws IOException
    
    //获取OutputStream对象
    public static OutputStream newOutputStream(Path path, OpenOption... options) throws IOException
    

    应用示例:

    package com.weiwei.xu;
    
    import org.junit.Test;
    
    import java.io.IOException;
    import java.nio.file.*;
    
    
    public class NIO2Test {
    
        @Test
        public void test() throws IOException {
            Path path1 = Paths.get("D:\project\nio\1.jpg");
            Path path2 = Paths.get("D:\project\nio\2.jpg");
    
            Files.copy(path1,path2, StandardCopyOption.COPY_ATTRIBUTES);
        }
    }
    
    

    5.4 自动资源管理

    • JDK7增加了一个新特性,该特性提供给了另外一种管理资源的方式,这种方式能自动关闭文件。这个特性被称为自动资源管理。该特性以try语句的扩展板为基础。自动资源管理主要用于,当不再需要文件或其他资源的时候,可以防止无意中忘记释放它们。

    • 语法:

    try(需要关闭的资源声明){
        //可能发生异常的语句
    }catch(异常类型 变量名){
        //异常处理语句
    }
    ……
    finally{
        //一定执行的语句
    }
    
    • 当try代码块结束时,自动释放资源。因此不需要显示的调用close()方法。

    注意:

    ①try语句中声明的资源隐式的声明为final类型,资源的作用仅限于try语句块。

    ②可以在一条try语句中管理多个资源,每个资源用;隔开即可。

    ③需要关闭的资源,必须实现了AutoCloseable接口或Closeable接口。

    应用示例:

    package com.weiwei.xu;
    
    import org.junit.Test;
    
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.file.*;
    
    
    public class NIO2Test {
    
        @Test
        public void test() throws IOException {
            
            try(FileChannel inChannel = FileChannel.open(Paths.get("D:\project\nio\1.jpg"), StandardOpenOption.READ);
                FileChannel outChannel = FileChannel.open(Paths.get("D:\project\nio\2.jpg"), StandardOpenOption.WRITE, StandardOpenOption.CREATE)){
    
                ByteBuffer buf = ByteBuffer.allocate(1024);
                inChannel.read(buf);
                buf.flip();
    
                outChannel.write(buf);
    
            }catch(IOException e){
    
            }
    
        }
    
    }
    
    
  • 相关阅读:
    第八节:layout和partialView、RenderPartial和Partial、 action和RenderAction
    第七节:HtmlHelper及其扩展
    第六节:Razor引擎及相关语法
    第五节:从源码的角度理解各种Result(ActionResult、JsonResult、JavaScriptResult等)
    图片保存到数据库以及C#读取图片
    C#复制数据到剪切板
    IIS8.5支持WCF
    C# 中const和readonly的区别
    C#获取类库(DLL)的绝对路径
    C#怎样用文件读写在文件的原有基础上追加一行数据
  • 原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/13660460.html
Copyright © 2011-2022 走看看