Java:如何修改控制台输出?
我需要编写一个简单的基于终端的程序,该程序应该
- 使用包装在 BufferedReader 中的 FileReader 从文件中读取一些文本。
- 将此文本打印到控制台,用户应该能够修改它。
- Enter + S 后,程序应读取修改后的文本并将其保存回原始文件。
总而言之,我们的想法是为没有 GUI 的类 Unix 环境编写一个文本编辑器。
但是,我在执行步骤 2 和 3 时遇到问题。
System.out 打印的所有文本都是不可变的。我怎样才能改变这种行为?
如何实现 Enter + S 的按键侦听器。我可以使用 GUI 轻松实现,但是如何在基于控制台的程序中添加侦听器?在 Enter 上,程序应该简单地向修改后的文本添加一个新行。 另外,我如何读取已经打印到终端的文本?
PS 我必须使用 JDK 1.4,因此无法访问 Scanner 和 Console 类(如果有相关的话)。我还认为这样的任务在 Java 中是不可能完成的,所以你可以建议用 C/C++ 解决方案。毕竟,他们在 MS-DOS 时代就以某种方式做到了。
谢谢你!
I need to write a simple terminal-based program that should,
- Read some text from a file using FileReader wrapped in BufferedReader.
- Print this text to console and a user should be able to modify it.
- Upon Enter + S, the program should read the modified text and save it back to the original file.
To sum up, the idea is to write a text editor for Unix-like environments with no GUI.
However, I have problems with steps 2 and 3.
All the text that is printed by System.out is immutable. How can I change this behaviour?
How do I implement a key listener for Enter + S. I can easily do it with GUI, but what do I add a listener to in a console-based program? And on Enter the program should simply add a new line to the modified text.
Also, how can I read text that has already been printed to the terminal?
P.S. I have to use JDK 1.4 so no access to Scanner and Console classes if it is any relevant. I also think that such a task would be impossible to do in Java, so you could suggest a solution in C/C++ After all, they did it somehow back in MS-DOS era.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要的是类似 charva 的东西。它是一个 Java 库,它执行 24 x 80 终端时代旧的 termcap / termio /curses / ncurses 库所做的事情。
Charva(和curses等)通过向终端或终端仿真器发送控制代码和/或转义序列来移动光标、写入和擦除字符等来工作。在现代系统上,它们依赖于提供 ANSI 或 VT100 终端仿真的控制台......或类似的东西。
What you need is something like charva. It is a Java library that does what the old termcap / termio / curses / ncurses libraries did in the days of 24 x 80 terminals.
Charva (and curses, etc) work by sending control codes and/or escape sequences to the terminal or terminal emulator to move the cursor around, write and erase characters and so on. On a modern system, they depend on the console providing ANSI or VT100 terminal emulation ... or something like that.
这些事情是在 Unix 中使用 Curses 库完成的:http://en.wikipedia。 org/wiki/Curses_(programming_library)。
These kinds of things are done in Unix with the curses library: http://en.wikipedia.org/wiki/Curses_(programming_library).
http://sourceforge.net/projects/javacurses/ 你需要类似的东西。尝试找到一些免费的javacurses库
http://sourceforge.net/projects/javacurses/ you need something like that. Try to find some free java curses library