没有输入时扫描程序冻结程序

Scanner Freezes Program When No Input Is Given

我正在制作一个主要用于管道的应用程序。在程序开始时,这是我如何获得所有输入的:

Scanner scanner = new Scanner(System.in);
List<String> input = new ArrayList<>();

while (scanner.hasNextLine()) {
    input.add(scanner.nextLine());
}

scanner.close();

与管道一起使用时,效果很好。例如,运行ning ls | java -jar ... 它打印出 ls 给出的所有行。但是,当我只是 运行 java -jar ... 而没有 input/pipe 时,它会冻结并且没有任何反应。即使当我按下回车键并敲击我的键盘时,它仍然卡住了。我只能使用 ctrl-c 来停止程序。我认为这是因为没有什么可读的,所以 Scanner 一直等到有东西可读。如何在不冻结的情况下读取 System.in 中的所有行,或者有更好的方法吗?谢谢。

编辑: 将此标记为重复的人显然没有阅读我的问题。我的问题是检查 System.in 是否为空以不冻结程序,而不是如何通过输入阶段。它很冷,因为 System.in 中没有任何内容,所以扫描仪一直在运行,但我想检查 System.in.

中是否没有任何内容

回答: 谢谢@QiuZhou 的回答。我修改了他的第二个例子来得到这个:

List<String> input = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

while (reader.ready()) {
    String nextLine = reader.readLine();
    input.add(nextLine);
}

它就像一个魅力。

它很冷,因为正如您所说,没有可读取的内容,所以扫描程序一直等到有内容可读取为止,但您想要读取所有行来自System.in而不冻结,这对我来说听起来有点矛盾,这不像从文件中读取,我们不知道使用你的程序的人会输入多少行,除非人告诉我们。我建议你定义一个特殊字符为end of input,例如done,当这个人决定他已经完成时,输入done结束等待:

        Scanner scanner = new Scanner(System.in);
        List<String> input = new ArrayList<>();

        System.out.println("Please enter something. Enter 'done' when finished.");

        while (scanner.hasNextLine()) {
            String nextLine = scanner.nextLine();
            if(nextLine.equals("done"))break;
            input.add(nextLine);
        }

        scanner.close();

要检查 System.in 中是否有内容可读而不会卡在那里,您可以使用 BufferedReader.ready() 来完成,如文档所述,请注意 in.ready() 将 return 为真当且仅当流上的下一次读取不会阻塞。

        System.out.println("Please enter something. Enter 'done' when finished.");

        List<String> input = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while(true) {
            if (reader.ready()) {
                String nextLine = reader.readLine();
                if(nextLine.equals("done"))break;
                input.add(nextLine);
            }
            //since ready() will not block, you can add additional logic here, like if it
            //takes too long to read next line from input, you can just end here
            // break;
        }