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

廉价感情. 提交于 2021-01-27 06:06:42

问题


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):

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-e and other keys without repeatable behavior, but the loop is interrupted randomly)


回答1:


Control + D should send the EOF character as excepted , but it is an bug in 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 + Z sends the EOF character.




回答2:


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

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.




回答3:


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.



来源:https://stackoverflow.com/questions/23742421/what-is-the-end-of-file-stream-keyboard-combination-to-use-with-system-in-read

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!