Home > Java > javaTutorial > How to use NIO functions for efficient IO operations in Java

How to use NIO functions for efficient IO operations in Java

WBOY
Release: 2023-06-26 17:06:10
Original
1106 people have browsed it

With the rapid development of Internet technology, the demand for various large-scale application systems continues to increase, and the need for efficient IO operations is becoming more and more urgent. As a commonly used programming language, Java is increasingly used in IO operations. The NIO function, as an implementation method of efficient IO operations, has also attracted much attention in recent years. This article will introduce how to use NIO functions in Java to perform efficient IO operations.

1. Introduction to NIO

NIO, New I/O, is a new IO API introduced in Java 1.4 version. Compared with the traditional IO API, NIO is non-blocking. IO operations. The traditional IO API is stream-oriented, while NIO is block-oriented. The disadvantage of streaming IO is that when a large file needs to be read and written, many reading and writing IO blocking problems will occur, seriously affecting the efficiency of the program. Block IO can avoid unnecessary IO blocking and improve IO operation efficiency when reading and writing a data block. At the same time, NIO also provides an efficient multiplexing (Multiplexing) mechanism, which can monitor multiple IO events at the same time and improve the efficiency of network communication.

2. NIO Usage

  1. Buffer class in NIO

The Buffer class in NIO is one of the core classes, and its function is to cache reading and writing data. Different from the traditional IO API, Buffer in NIO has certain rules for reading and writing data. For example, before writing data, you need to call the Buffer.flip() method to reset the write pointer of the buffer for reading operations. The Buffer class has many other methods, such as position(), limit(), capacity(), etc., which can be used as needed. In addition, there are a variety of Buffer classes in NIO, such as ByteBuffer, CharBuffer, IntBuffer, etc., used to cache different types of data.

  1. Channel class in NIO

In addition to the Buffer class, the Channel class is also one of the core classes in NIO, and its function is to read and write data. The Channel class in NIO includes various types of channels, such as FileChannel, DatagramChannel, etc. Unlike the traditional IO API, the Channel class in NIO can perform non-blocking IO operations.

  1. Selector class in NIO

The Selector class in NIO is the key class to implement multiplexing in NIO. The Selector class can monitor multiple Channels. When one or more Channels have data that can be read or written, the Selector will notify the corresponding Channel to perform read and write operations. Using the Selector class can avoid creating multiple threads to read and write multiple Channels, thereby improving program efficiency.

3. NIO Example

The following uses an example to illustrate the use of NIO. Suppose there is a file, and the data in the file needs to be read line by line and output to the console.

  1. Read file content

FileChannel can read file content through the following methods:

public static void readFile(String fileName) throws IOException{
        FileInputStream fis = new FileInputStream(fileName);
        FileChannel fc = fis.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while(fc.read(buffer) != -1){
            buffer.flip();
            while(buffer.hasRemaining()){
                System.out.print((char)buffer.get());
            }
            buffer.clear();
        }

        fc.close();
        fis.close();
    }
Copy after login

In the code, the file channel FileChannel is first obtained through FileInputStream, Then create a ByteBuffer buffer and specify the buffer size as 1024 bytes. When reading a file, read it through the fc.read(buffer) method and determine whether the reading is completed. If the reading is not completed, call the buffer.flip() method to reset the position and limit of the buffer. After each loop reads the data in the buffer, you need to set the position of the buffer to 0 and the limit to the capacity of the buffer, so that the buffer can be reused.

  1. Implement line-by-line reading

You can use the LineIterator class to implement line-by-line reading:

public static void readLine(String fileName) throws IOException{
        FileInputStream fis = new FileInputStream(fileName);
        FileChannel fc = fis.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        Charset charset = Charset.forName("UTF-8");
        LineIterator iterator = new LineIterator(charset.newDecoder());

        while(fc.read(buffer) != -1){
            buffer.flip();
            iterator.read(buffer, new LineHandler() {
                @Override
                public boolean handle(String line) throws Exception {
                    System.out.println(line);
                    return true;
                }

                @Override
                public void endOfFile() throws Exception {
                    System.out.println("End of File.");
                }
            });
            buffer.compact();
        }

        iterator.finish();

        fc.close();
        fis.close();
    }
Copy after login

In the code, first create a LineIterator object, and The specified character set encoding is UTF-8. When reading the file content, read the file content line by line through the iterator.read(buffer,LineHandler) method. The handle(String line) method in the LineHandler interface is used to process a line of data read, and the endOfFile() method is used to process the situation when the file reading ends. In the handle method, you can process a line of data you read, such as outputting it to the console.

  1. Using the Selector class

You can use the Selector class to implement multiplexing operations. The following is a simple example:

public static void selectorSocket() throws IOException {
        Selector selector = Selector.open();
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress("localhost", 9999));
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            int readyChannels = selector.select();
            if (readyChannels == 0) continue;

            Set<SelectionKey> selectedKeys = selector.selectedKeys();
            Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
            while (keyIterator.hasNext()) {
                SelectionKey key = keyIterator.next();
                if (key.isAcceptable()) {
                    ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
                    SocketChannel socketChannel = serverChannel.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector, SelectionKey.OP_READ);
                } else if (key.isReadable()) {
                    SocketChannel socketChannel = (SocketChannel) key.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    socketChannel.read(buffer);
                    buffer.flip();
                    while (buffer.hasRemaining()) {
                        System.out.print((char) buffer.get());
                    }
                    buffer.clear();
                }
                keyIterator.remove();
            }
        }
    }
Copy after login

Create first in the code A Selector and register a ServerSocketChannel channel to listen for connections on port 9999. In the while loop, monitor IO events through the Selector's select() method. When one or more IO events registered by the Channel are triggered, the Selector will return the corresponding SelectionKey. You can use the SelectionKey.isAcceptable() method to determine the type of SelectionKey and perform operations, such as registering the OP_READ operation of a SocketChannel.

4. Summary

This article introduces how to use NIO functions in Java to perform efficient IO operations. By introducing the NIO mechanism, the blocking problem of traditional IO can be avoided and the efficiency of the program can be improved. The core classes in NIO include Buffer, Channel, Selector, etc., through which various efficient IO operations can be completed. In practical applications, the form and method of using NIO functions need to be determined according to specific business needs and scenarios to obtain the best results.

The above is the detailed content of How to use NIO functions for efficient IO operations in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template