Java 与 System.in.read() 一起使用的文件结束/流键盘组合是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23742421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:57:29  来源:igfitidea点击:

What is the End Of File/Stream keyboard combination to use with System.in.read()

javaeofconsole-input

提问by mins

Apologize if this trivial question has already been answered, I cannot find it at SO. Reading lines from the IDE console with this Java trivial code (Windows 7 and Eclipse Kepler):

如果这个微不足道的问题已经得到回答,请道歉,我在 SO 上找不到。使用此 Java 简单代码(Windows 7 和 Eclipse Kepler)从 IDE 控制台读取行:

int v;
try { while ((v = System.in.read()) != -1) System.out.println(v); }
catch (IOException e) { ; }

How the user can make the value v equal to -1? (I've tried Ctrl+ d-z-x-c-s-eand other keys without repeatable behavior, but the loop is interrupted randomly)

用户如何使值 v 等于 -1?(我试过Ctrl+ d- z- x- c- s-e和其他没有可重复行为的键,但循环被随机中断)

采纳答案by Mani

Control+ Dshould send the EOF character as excepted , but it is an bug in Eclipse.

Control+D应该发送 EOF 字符作为异常,但它是 Eclipse 中的一个错误。

One of the user reported as

其中一位用户报告为

    In Kepler 4.3 eclipse.buildId=4.3.0.M20130911-1000 on Linux the problem 
    still exists in the Java console. I found the following workaround:

    If you leave the console to focus on another view, and then refocus on the console,
    then Ctrl-D (EOF) works as expected.

Follow here

按照这里



When using Eclipse in Windows, Control + Zsends the EOF character.

在 Windows 中使用 Eclipse 时,Control + Z发送 EOF 字符。

回答by Pankaj Gadge

Use the control-D character(type D while holding down the control key) to indicate to a program reading from the standard input stream that you have finished entering input. The control-D character is often written as ^D.

使用control-D 字符(在按住 control 键的同时键入 D)向从标准输入流中读取的程序指示您已完成输入。control-D 字符通常写为^D

回答by rom99

Testing this out using Groovy in a Windows command prompt, ctrl-D doesn't work while ctrl-C does:

在 Windows 命令提示符下使用 Groovy 对此进行测试,ctrl-D 不起作用而 ctrl-C 起作用:

C:>groovy -e "while(v = System.in.read()){ println v }"
^D
4
13
10
-1
Terminate batch job (Y/N)? y

First I hit ctrl-D then enter, resulting in output ctrl-D, 4, 13, 10 (the last three being EOT, CR, LF, I guess, not sure what ^D amounts to in this case). Then I tried ctrl-C and the '-1' end-of-input was sent. So it seems this is dependent on the shell as Dev says.

首先我按 ctrl-D 然后回车,结果是 ctrl-D, 4, 13, 10 (最后三个是 EOT, CR, LF, 我猜,不确定 ^D 在这种情况下是什么)。然后我尝试了 ctrl-C 并发送了“-1”输入结束符。因此,正如 Dev 所说,这似乎取决于 shell。