Java:我如何修改控制台输出? [英] Java: how can I modify console output?

查看:746
本文介绍了Java:我如何修改控制台输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个简单的基于终端的程序,应该使用


  1. 使用包装在BufferedReader中的FileReader从文件中读取一些文本。

  2. 将此文本打印到控制台,用户应该能够修改。

  3. Enter + S后,程序应读取修改的文本


  4. 总而言之,想法是为类Unix环境写一个文本编辑器



    但是,我对步骤2和3有问题。


    1. System.out打印的所有文本都是不可变的。如何更改此行为?


    2. 如何实现Enter + S的键监听器。我可以轻松地使用GUI,在基于控制台的程序中的监听器?在输入程序应该简单地添加一个新行到修改的文本。
      此外,我如何阅读已经打印到终端的文本?


    我必须使用JDK 1.4,所以没有访问Scanner和控制台类,如果它是任何相关的。我也认为这样的任务在Java中是不可能做到的,所以你可以在C / C ++中建议一个解决方案。毕竟他们以某种方式回到MS-DOS时代。



    谢谢!

    解决方案

    您需要的是像 charva 。它是一个Java库,在24 x 80终端的日子里完成什么旧的termcap / termio / curses / ncurses库。



    Charva(和curses等)通过向终端或终端仿真器发送控制代码和/或转义序列来移动光标,写入和擦除字符等来工作。在现代系统上,他们依赖于提供ANSI或VT100终端仿真...或类似的控制台。


    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!

    解决方案

    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.

    这篇关于Java:我如何修改控制台输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆