NIO之二--Channel

Java NIO Channel

  • Channel Implementations
  • Basic Channel Example

Java NIO Channels are similar to streams with a few differences:

  • You can both read and write to a Channels. Streams are typically one-way (read or write). 1、Channel可双向操作,读写
  • Channels can be read and written asynchronously. 2、读写可异步
  • Channels always read to, or write from, a Buffer. 3、一般跟Buffer操作

As mentioned above, you read data from a channel into a buffer, and write data from a buffer into a channel. Here is an illustration of that:

NIO之二--Channel_第1张图片
overview-channels-buffers.png

Java NIO: Channels read data into Buffers, and Buffers write data into Channels

Channel Implementations

Here are the most important Channel implementations in Java NIO:

  • FileChannel:读写文件
  • DatagramChannel: UDB中读写数据
  • SocketChannel: TCP中读写数据
  • ServerSocketChannel:监听TCP连接,每次一个连接过来,都会创建一个SocketChannel

The FileChannel reads data from and to files.

The DatagramChannel can read and write data over the network via UDP.

The SocketChannel can read and write data over the network via TCP.

The ServerSocketChannel allows you to listen for incoming TCP connections, like a web server does. For each incoming connection a SocketChannel is created.

Basic Channel Example

Here is a basic example that uses a FileChannel to read some data into a Buffer:

public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("nio-data.txt", "rw"); // 获取一个随机读写文件的引用

        FileChannel inChannel = randomAccessFile.getChannel(); // 从文件引用中获取 channel,用于读取数据

        ByteBuffer buf = ByteBuffer.allocate(48);  // buffer是一块内存区域,限定每次取48字节

        int bytesRead = inChannel.read(buf); // 第一次从channel中读取,48字节

        while (bytesRead != -1) {  // 判断是否已到文件末尾
            System.out.println("Read:" + bytesRead);  // 打印读到的字节长度

            buf.flip(); // 翻转,这样可以从buffer中读取数据出来

            while (buf.hasRemaining()) {   // 当buffer中有数据时,循环,使用get读取单字节并且打印出来
                System.out.print((char) buf.get());  // 注意,get并不会取出数据,而只会读数据,然后offset后移一位
            }
            System.out.println(); // 换行

            buf.clear(); // clear掉buffer内容,不然无法继续读取新内容
            bytesRead = inChannel.read(buf); // 继续从文件中读取数据
        }

        randomAccessFile.close(); // 关闭文件
    }

Notice the buf.flip() call. First you read into a Buffer. Then you flip it. Then you read out of it. I'll get into more detail about that in the next text about Buffer's.

注意flip,在这里看都翻译成翻转,意思是本来是往buffer中写入数据的,执行完flip后,就可以从中读取数据。如果不执行,无法从buf中读取到数据。

执行结果如下:

结果1,注释掉flip,可以看到无法拿到buf中的数据,每次clear后重新读取

Read:48

Read:48

Read:48

Read:29
!
all money go my h

结果2,正常结果:

Read:48-----------
when I get a lot of money, I will leave job and 
Read:48-----------
enjoy my life
that what all i need is money ,mon
Read:48-----------
ey,money
all money go my home!
all money go my h
Read:29-----------
ome!!
all money go my home!!!

你可能感兴趣的:(NIO之二--Channel)