【问题标题】:What is the End Of File/Stream keyboard combination to use with System.in.read()与 System.in.read() 一起使用的文件结尾/流键盘组合是什么
【发布时间】:2014-05-19 16:08:00
【问题描述】:

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

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

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

【问题讨论】:

  • 假设这是一个int
  • 我相信这取决于控制台。
  • 我使用 Windows,并在 Eclipse 中运行代码,如果有帮助的话。

标签: java eof console-input


【解决方案1】:

Control + D 应该将 EOF 字符作为例外发送,但这是 Eclipse 中的一个错误。

其中一位用户报告为

    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.

关注here


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

【讨论】:

  • 非常感谢,太棒了!我必须使用 Ctrl-Z (Windows)。更改视图,例如我选择问题选项卡,然后选择控制台,允许 CTRl-Z 识别...哇。
  • 5 年后,这仍然没有在新的 2021-03 版本中得到修复,它使得自动化标准的运行配置选项需要手动发送 EOF 字符。
【解决方案2】:

在 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

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

【讨论】:

  • Ctrl-C 对我不起作用,但 Ctrl-Z 可以(在 Eclipse 控制台中)。我有 Mani 提到的错误,需要更改焦点视图以考虑此输入。
【解决方案3】:

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

【讨论】:

  • 在 Mac 上是 Cmd+D (⌘+D)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
  • 1970-01-01
  • 2014-07-01
相关资源
最近更新 更多