从Java System.in读取输入的问题

从Java System.in读取输入的问题,java,io,Java,Io,我正在尝试编写一个方法,该方法在命令行上提示用户输入,并从stdin中将其输入作为字符串读取,然后返回。我第一次给它打电话时,一切正常。之后对getInput()的所有调用都不返回任何内容 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Prompts the user for input and reads from standard inp

我正在尝试编写一个方法,该方法在命令行上提示用户输入,并从stdin中将其输入作为字符串读取,然后返回。我第一次给它打电话时,一切正常。之后对getInput()的所有调用都不返回任何内容

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

/**
 * Prompts the user for input and reads from standard input (stdin).
 * Note: Always check if the return is null!
 * 
 * @param description Describes the user input.
 * @return A String of the input, or null when failed.
 */
private String getInput(String description)
{       
    System.out.print(description + ": ");
    String input = null;

    InputStreamReader stream = null;
    BufferedReader reader = null;
    try {
        // Open a stream to stdin
        stream = new InputStreamReader(System.in);

        // Create a buffered reader to stdin
        reader = new BufferedReader(stream);

        // Try to read the string
        input = reader.readLine();

        // Exhaust remainder of buffer
        while (reader.skip(1) > 0) {
            // Do nothing
        }

    } catch (IOException e) {
        e.printStackTrace();
        // Error reading input

    } finally {
        // Clean up readers and streams
        try {
            if (reader != null) {
                reader.close();
            }
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
        }
    }

    System.out.print("\n");
    return input;
}

/**
 * Display the login prompt.
 */
private boolean promptLogin()
{       
    // prompt for user name and password
    String user = getInput("Enter username");
    String pass = getInput("Enter password");

    if (user == null || pass == null) {
        System.out.println("Invalid login information.");
        return false;
    }
    // ..
     }

不能关闭标准输入流;这就是为什么它只有第一次起作用

/**
 * Prompts the user for input and reads from standard input (stdin).
 * Note: Always check if the return is null!
 * 
 * @param description Describes the user input.
 * @return A String of the input, or null when failed.
 */
private String getInput(String description) {
    System.out.print(description + ": ");
    String input = null;

    InputStreamReader stream = null;
    BufferedReader reader = null;
    try {
        // Open a stream to stdin
        stream = new InputStreamReader(System.in);

        // Create a buffered reader to stdin
        reader = new BufferedReader(stream);

        // Try to read the string
        input = reader.readLine();           
    } catch (IOException e) {
        e.printStackTrace();
    } 

    return input;
}

/**
 * Display the login prompt.
 */
private boolean promptLogin() {
    // prompt for user name and password
    String user = getInput("Enter username");
    String pass = getInput("Enter password");

    if (user == null || pass == null) {
        System.out.println("Invalid login information.");
        return false;
    }

    return true;
}

有点相切,Java的
控制台
类有一个
readPassword
方法,可能比这个方法更安全。