Home  >  Article  >  Java  >  Java uses the readLine() function of the BufferedReader class to read console input line by line

Java uses the readLine() function of the BufferedReader class to read console input line by line

PHPz
PHPzOriginal
2023-07-24 15:09:302080browse

Java uses the readLine() function of the BufferedReader class to read console input line by line

In Java programming, we often need to read user input from the console. Java provides the BufferedReader class to read input line by line from the console. This function is very useful for programs that need to read the user's command line input and perform interactive operations. Below we will introduce in detail how to use the readLine() function of the BufferedReader class to read console input line by line.

First, we need to introduce the two classes BufferedReader and IOException in the java.io package:

import java.io.BufferedReader;
import java.io.IOException;

Next, we need to create an InputStreamReader object in the program and pass it to BufferedReader Object to read console input. The code is as follows:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

Now, we can use the readLine() function of the BufferedReader class to read the console input line by line. This function returns the string read, or null if no input is available. The code is as follows:

String input;
try {
    while ((input = reader.readLine()) != null) {
        // 处理每一行输入
        System.out.println("输入的内容是:" + input);
    }
} catch (IOException e) {
    e.printStackTrace();
}

In the above code, we use a while loop to continuously read the console input until null is encountered. In each loop, we can process each line of input and simply output it to the console.

The complete sample code is as follows:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ConsoleInputExample {
    public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String input;
        try {
            while ((input = reader.readLine()) != null) {
                System.out.println("输入的内容是:" + input);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

When we run the above sample code, the program will wait for our input. We can enter the content line by line and press the Enter key to confirm the input. Each time a line is entered, the program prints it to the console.

By using the readLine() function of the BufferedReader class, we implement the function of reading input line by line from the console. This provides us with great convenience when writing interactive programs, and can also be used to handle command line parameters and other scenarios where console input needs to be read.

Summary:

This article introduces how to use the readLine() function of the BufferedReader class in Java to read console input line by line. We first introduced the relevant classes, then created the BufferedReader object and passed in the input stream object, and finally used the while loop and readLine() function to read the input content line by line. I hope this article will help you understand and use the BufferedReader class.

The above is the detailed content of Java uses the readLine() function of the BufferedReader class to read console input line by line. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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