Java: how can I modify console output?

2019-01-28 09:23发布

I need to write a simple terminal-based program that should,

  1. Read some text from a file using FileReader wrapped in BufferedReader.
  2. Print this text to console and a user should be able to modify it.
  3. 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.

  1. All the text that is printed by System.out is immutable. How can I change this behaviour?

  2. 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!

3条回答
我只想做你的唯一
2楼-- · 2019-01-28 09:29

http://sourceforge.net/projects/javacurses/ you need something like that. Try to find some free java curses library

查看更多
爷的心禁止访问
3楼-- · 2019-01-28 09:30

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.

查看更多
叼着烟拽天下
4楼-- · 2019-01-28 09:36

These kinds of things are done in Unix with the curses library: http://en.wikipedia.org/wiki/Curses_(programming_library).

查看更多
登录 后发表回答