问题
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