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

Jun 26, 2023 pm 05:06 PM
java nio io operation

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles