Read Input until control+d

点点圈 提交于 2019-12-18 07:24:44

问题


I want to prompt the user to begin entering characters and I want them to be able to enter characters as long as they want until they hit control+d to exit.

For example, they can type a string of numbers like: 1234567 and as soon as they decide to hit control+d the line that they entered will be displayed (so without having to hit return)

I am thinking I'll need a buffered reader or something. Any suggestions?


回答1:


What rlibby said is spot on: the CTL-D will cause the terminal to flush buffered input to the JVM. However, the keypress event itself is captured and acted on by the terminal and not passed through.

Fortunately, though, it's easy to detect. If the user hits CTL-D on a line of its own, there is no input to flush...which to the JVM is indistinguishable from EOF. Accordingly, System.in.read() will return -1 per the contract of InputStream. If you've wrapped System.in with a BufferedReader, readLine() will return null.

This is my main loop for an interactive command line tool I just wrote:

BufferedReader systemIn = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));

String line;
while((line = systemIn.readLine()) != null) {
    // my program loop.
}

One thing worth pointing out is that if the user hits CTL-D after inputting characters (but before hitting return), you'll get those characters. I don't believe there's a way to detect CTL-D when it's not on a line of its own.

DISCLAIMER: I have no idea how this applies to Windows.




回答2:


http://download.oracle.com/javase/6/docs/api/java/io/BufferedInputStream.html#read%28%29

public class InputTest {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringBuilder out = new StringBuilder();
    while (true) {
      try {
        int c = in.read();
        if (c != 4)  // ASCII 4 04 EOT (end of transmission) ctrl D, I may be wrong here
            out.append (c);
        else
            break;
      } catch (IOException e) {
        System.err.println ("Error reading input");
      }
    }
    System.out.println(out.toString());
  }
}



回答3:


You are confusing the roles of your Java program and of the terminal. The terminal buffers input and occasionally supplies it to the Java program, especially after line feeds. Ctrl+D also causes the terminal to make buffered input available, or, if nothing is buffered, to terminate the input. Asking for a program to do something on Ctrl+D is essentially asking it to do something when it reads all available input (and more may become available later). Buffering input on the Java side is going to make things more complicated, rather than less.




回答4:


In a GUI, you can use a KeyListener.




回答5:


give this code a try. it actually worked for me

    // press ctrl+Z(windows) and ctrl+D(mac, linux) for input termination
    StringBuilder out = new StringBuilder();
    String text = null;
    Scanner scanner = new Scanner( System.in );
    while( scanner.hasNextLine() )
    {
        text = new String( scanner.nextLine() );
        out.append( text );
    }
    scanner.close();
    System.out.println( out );
    System.out.println( "program terminated" );


来源:https://stackoverflow.com/questions/5837823/read-input-until-controld

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