【发布时间】:2020-02-22 20:47:42
【问题描述】:
我目前正在为一个打算介绍套接字编程和多线程编程的学校项目编写一个小型 CLI。我们基本上是在实现一种文件传输协议。我目前在格式化 CLI 时遇到了一些问题。由于程序的多线程特性,有时会请求输入,然后显示输出。这使得用户和 CLI 之间的整体交互变得尴尬,因为现在用户必须在输出发生后输入输入。
我想知道是否有一种方法可以通过标准 Java I/O 将输入始终保持在 CLI 的底部,即使有新消息进入也是如此。
例如,当前的输出如下所示:
Connected to 127.0.0.1:5000 and 127.0.0.1:5001
mytftp> pwd
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
mytftp> pwd &
mytftp>
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
ls
.DS_Store
sdf
clientFile.txt
mytftp>
但我期待这样的事情:
Connected to 127.0.0.1:5000 and 127.0.0.1:5001
mytftp> pwd
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
mytftp> pwd &
/Users/Shawn/Desktop/Computer Science/CSCI4780 Distributed Systems/Project2/server
mytftp> ls
.DS_Store
sdf
clientFile.txt
mytftp>
注意:这里的“&”表示“在单独的线程上运行此命令”。这就是为什么在下一次执行期间,首先出现“mytftp>”提示,然后显示线程的结果。我只是希望当前提示始终保持在底部。旁注,pwd 命令上的 & 未添加以提高性能。它通常仅用于文件上传和下载命令,但此处仅用作示例。
我不确定这是否可行,但我们将不胜感激。
重申一下,流程想:
Connection Established: 连接到 127.0.0.1:5000 和 127.0.0.1:5001Command is entered into new thread:mytftp> pwd &Next prompt is shown ready for a new command:mytftp>
一旦pwd & 的结果出现,我希望提示下来:
Connection Established: 连接到 127.0.0.1:5000 和 127.0.0.1:5001Command is entered into new thread: mytftp> pwd &Space was made for the prompt and results shown: /Users/Shawn/Desktop/Computer Science/CSCI4780 分布式系统/Project2/serverNext prompt is shown ready for a new command:mytftp>
【问题讨论】:
标签: java io java.util.scanner