如何在Java中使用echo读取行?
我想使用带有一种 readLine() (或类似)的 BufferedReader ,它可以为每次按下的按键返回回声。
它用于远程终端。另一种询问方式是如何在 java 中实现控制台。
这是想到的,但太丑陋了。是否有任何已知的库可以实现类似的功能?
while(condition) {
nByteRead = in.read(buffer);
if (nByteRead != -1) {
// ECHO
out.write(buffer, 0, bytes_read);
// read bytes till NEW_LINE...
// etc...!
}
}
当然,我可以将这种行为封装在某个线程中,并继续为此使用一个库,我只是想知道是否已经发明了一些轮子。
感谢您的任何提示!
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. 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大多数终端,包括 Ubuntu 和 Windows 中的默认终端(我相信),在用户按回车键之前不会将字符传递给 JVM。 (即,它在系统的较低级别上以整行为基础进行缓冲。)
如果您需要从终端一次读取一个字符,则必须使用较低级别的系统库。
相关问题:
(免责声明,我不确定我是否正确理解了您的问题。)
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:
(disclamer, I'm not completely sure I understood your question correctly.)
有:
There's: