开发者

How to readLine with echo in Java?

开发者 https://www.devze.com 2023-04-01 15:50 出处:网络
I would like to use a BufferedReader with a kind of readLine() (or similar) that can return an echo for every keystroke pressed.

I would like to use a BufferedReader with a kind of readLine() (or similar) that can return an echo for every keystroke pressed.

It's for a remote terminal. Other way to ask it is how is implemented a console in java.

This is what came to mind but is too ugly. 开发者_JAVA百科Is there any known library that implement something like this?

while(condition) {

    nByteRead = in.read(buffer);

    if (nByteRead != -1) {

        //  ECHO
        out.write(buffer, 0, bytes_read);                      

        //  read bytes till NEW_LINE...                            
        //  etc...!                            
    }                           
}                    

Of course I could encapsulate this behaviour in some thread and go on with a library for this, I just wonder if there is some wheel already invented.

Thanks for any hint!


Most terminals, including the default terminals in Ubuntu and Windows (I believe) won't pass on the characters to the JVM until the user hits return. (I.e., it is buffered on a full-line basis on a lower level in the system.)

If you need to read one character at a time from the terminal, you'll have to go with a lower level system library.

Related question:

  • Why can't we read one character at a time from System.in?

(disclamer, I'm not completely sure I understood your question correctly.)


There's:

  • JLine: http://jline.sourceforge.net/
  • Java Curses: http://sourceforge.net/projects/javacurses/
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号