擦除 java 中的先前输出行

Erasing previous output lines in java

我一直在制作一个终端风格的...东西,(到目前为止)我仍然遇到的唯一问题是加载屏幕。我只是想添加它需要加载的错觉,没有什么太复杂的。我需要删除之前的输出行,这样它就会像加载屏幕一样加载。我在这里浏览了很多Q/As,我尝试的一切都没有帮助我总是得到随机文本或者文本不在它应该在的位置,或者它根本不起作用。我正在使用 Bluej。

当前代码:

public static void main(String [] args){
    try{
        System.out.println("▒█▀▀▀█ █▀▀█ █▀▀█ █▀▀█ ▀▀█▀▀ █▀▀█ █▀▀▄");
        Thread.sleep(1000);
        System.out.println("░▀▀▀▄▄ █░░█ █▄▄█ █▄▄▀ ░░█░░ █▄▄█ █░░█");
        Thread.sleep(1000);
        System.out.println("▒█▄▄▄█ █▀▀▀ ▀░░▀ ▀░▀▀ ░░▀░░ ▀░░▀ ▀░░▀");
        Thread.sleep(1000);
        System.out.println("o()xxxxxx[{:::::::::::::::::::::::::::::::>");
            Thread.sleep(1000);
            
            System.out.println("Loading... 0%");
            Thread.sleep(2000);
            //Code goes here
            System.out.println("Loading... 13%");
            Thread.sleep(2000);
            //Code goes here
            System.out.println("Loading... 42%");
            Thread.sleep(2000);
            //Code goes here
            System.out.println("Loading... 74%");
            Thread.sleep(4000);
            //Code goes here
            System.out.println("Loading... 92%");
            Thread.sleep(1000);
            //Code goes here
            System.out.println("Loading... 100%");
    }
    catch (InterruptedException e) {
        System.err.format("IOException: %s%n", e);
    }
}

我想要 post 完整的输出,但艺术不会与此混合,所以这就是它所说的,减去艺术:

斯巴达

正在加载... 0%

正在加载... 13%

正在加载... 42%

正在加载... 74%

正在加载... 92%

正在加载... 100%

它不应该显示多个“正在加载...”

我愿意接受任何合理的建议。

这应该可以解决问题:

public static void main(String [] args){
    try{
        System.out.println("▒█▀▀▀█ █▀▀█ █▀▀█ █▀▀█ ▀▀█▀▀ █▀▀█ █▀▀▄");
        Thread.sleep(1000);
        System.out.println("░▀▀▀▄▄ █░░█ █▄▄█ █▄▄▀ ░░█░░ █▄▄█ █░░█");
        Thread.sleep(1000);
        System.out.println("▒█▄▄▄█ █▀▀▀ ▀░░▀ ▀░▀▀ ░░▀░░ ▀░░▀ ▀░░▀");
        Thread.sleep(1000);
        System.out.println("o()xxxxxx[{:::::::::::::::::::::::::::::::>");
            Thread.sleep(1000);
            
            System.out.print("Loading... 0%xxxxxxxx");
            Thread.sleep(2000);
            //Code goes here
            System.out.print("3[2K");
            System.out.print("\rLoading... 13%");
            Thread.sleep(2000);
            //Code goes here
            System.out.print("3[2K");
            System.out.print("\rLoading... 42%");
            Thread.sleep(2000);
            //Code goes here
            System.out.print("3[2K");
            System.out.print("\rLoading... 74%");
            Thread.sleep(4000);
            //Code goes here
            System.out.print("3[2K");
            System.out.print("\rLoading... 92%");
            Thread.sleep(1000);
            //Code goes here
            System.out.print("3[2K");
            System.out.print("\rLoading... 100%");
    }
    catch (InterruptedException e) {
        System.err.format("IOException: %s%n", e);
    }
}

“\033[2K”删除当前行,“\r”使光标回到行首并覆盖已经写入的内容。

您也可以只使用“\r”,但它会有这样的行为:

System.out.print("Loading... 0%xxxx");
System.out.print("\rLoading... 13%");

结果输出将是 Loading... 13%xxx 因为第二个打印语句没有清除整行 - 它只是覆盖了已经存在的内容!

我猜你在找马车return:

public static void main(String[] args) throws Exception
{
    System.out.print("Loading... 10%");
    Thread.sleep(1000);
    System.out.print("\rLoading... 20%");
    Thread.sleep(1000);
            
}

have been making a terminal styled... thing

System.out.println("▒█▀▀▀█ █▀▀█ █▀▀█ █▀▀█ ▀▀█▀▀ █▀▀█ █▀▀▄");

问题是,System.out 是 'standard output' 而 不是终端 。当然,开箱即用,在几乎任何 OS 上,如果您键入 java -jar yourapp.jar,'standard output' 就会连接到终端,终端作为一个基本概念支持 overwriting/erasing 先前打印的文本。

问题是,'standard output' 的抽象允许您将几乎任何东西连接到标准输出。你可以写:java -jar yourapp.jar >/dev/printer 然后你的打印机将开始使用你的 asciiart 技能。

现在你看到问题了:有点棘手让打印机长出一双腿,跳下橱柜,跳到那个女孩的桌子上刚抓起一张写有您的 ascii 艺术字和 'loading' 文本的纸,然后取消打印文本。

这就是为什么 System.out 没有 eraseLine 方法的原因:并非所有接受 System.out 所代表的东西都支持这样的原则。

幸运的是 (?),终端确实支持它,但是,它们通过向它们发送 'commands' 来实现,这种形式是向它们发送创造性的字符序列,然后终端将其解释为 'oh, you want me to hop over to this position, change the background colour to green, the foreground colour to blue. Okay, got it'。

不幸的是,那里有很多终端(windows dos 框,但是一旦你进入 unix space,天哪,列表和我的腿一样长)并且这些终端在你必须发送什么魔术序列。

因此,获得一个处理它的库。我相信您会对此非常感兴趣:Lanterna.