在java中,可以使用从控制台或键盘读取输入:

  • java.util.Scanner
  • java.io.BufferedReader
  • java.io.Console

使用java.util.Scanner从控制台读取输入

以下是使用Scanner类从控制台读取输入的示例。

文件:ConsoleInputExample1.java -

package com.yiibai.tutorial.io;

import java.util.Scanner;

/**
 * @author yiibai
 */
public class ConsoleInputExample1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Read String
        System.out.println("Enter your name.");
        String name = scanner.nextLine();
        System.out.println("Your name is : " + name);

        // Read Int
        System.out.println("Enter your age.");
        int age = scanner.nextInt();
        System.out.println("Your age is : " + age);

        scanner.close();
    }
}

执行上面示例代码,得到以下结果:



使用java.io.BufferedReader从控制台读取输入

以下示例使用BufferedReader类的readLine()方法从控制台读取输入字符串。

文件:ConsoleInputExample2.java -

package com.yiibai.tutorial.io;

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

/**
 * @author yiibai
 */
public class ConsoleInputExample2 {
    public static void main(String[] args) {
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(System.in));

            // Read first line
            System.out.println("Enter first line");
            String line = bufferedReader.readLine();
            System.out.println("First line is : " + line);

            // Read second line
            System.out.println("\nEnter second line");
            line = bufferedReader.readLine();
            System.out.println("Second line is : " + line);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

执行上面示例代码,得到以下结果:

Enter first line
This is an example of reading input
First line is : This is an example of reading input

Enter second line
from console using buffered reader.
Second line is : from console using buffered reader.

使用java.io.Console读取输入

以下示例不适用于IDE。可以从窗口命令提示符运行此示例。

package com.yiibai.tutorial.io;

import java.io.Console;

/**
 * @author yiibai
 */
public class ConsoleInputExample3 {
    public static void main(String[] args) {
        Console console = System.console();

        if (console != null) {
            // Read String
            System.out.println("Enter username: ");
            String name = console.readLine();
            System.out.println("Username is : " + name);

            // Read password
            System.out.println("Enter your password.");
            char[] password = console.readPassword();
            System.out.println("Password is : " + new String(password));
        }

    }
}

执行上面示例代码,得到以下结果:

Enter username:
yiibai
Username is : yiibai
Enter your password.

Password is : abcd@1234