Posted on 2016-04-21 17:06
Milo的海域 阅读(405)
评论(0) 编辑 收藏 所属分类:
Java
java里如何修改console的历史输出信息呢?如果是当前行的修改可以简单想到"\r"的方案,但是如果要修改上一行呢? google了下原来还是有方法的,需要用到ansi的control sequences
ANSI code用java写了个简单的例子,例子就是把曾经的output修改为其他字符串并恢复之后的打印,代码里加了sleep,主要方便理解各种控制序列的含义
//print some test messages
System.out.println("1");
Thread.sleep(1000);
System.out.println("22");
Thread.sleep(1000);
System.out.println("333");
Thread.sleep(1000);
System.out.println("4444");
Thread.sleep(1000);
/**
* modify "333" to "-"
*/
// Move up two lines
int count = 2;
System.out.print(String.format("\033[%dA", count));
Thread.sleep(1000);
// Erase current line content
System.out.print("\033[2K");
Thread.sleep(1000);
// update with new content
System.out.print("-");
Thread.sleep(1000);
// Move down two lines
System.out.print(String.format("\033[%dB", count));
Thread.sleep(1000);
// Move cursor to left beginning
System.out.print(String.format("\033[D", count));
// continue print others
Thread.sleep(1000);
System.out.println("55555");
Thread.sleep(1000);