如何在Java中使用清屏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19142548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:28:31  来源:igfitidea点击:

How to use clear screen in Java

java

提问by Hiren Raiyani

How to clear the screen in Java?

如何在Java中清除屏幕?

I have created one menu driven simple demo program using while loop and switch case. After one loop completes, I want to clear a screen but that's not working. I will be glad for a possible solution to this.

我使用 while 循环和 switch case 创建了一个菜单驱动的简单演示程序。一个循环完成后,我想清除屏幕,但这不起作用。我会很高兴为此提供可能的解决方案。

I am using JDK 7 and running the program in command prompt.

我正在使用 JDK 7 并在命令提示符下运行该程序。

import java.util.*;
class DemoPrg
{
    public static void main(String argv[])
    {
        int ch;
        Scanner sc=new Scanner(System.in);
        while(true)
        {
            // i want to clear the scrren hear
            System.out.println("1. Insert New Record");
            System.out.println("2. Display Record");
            System.out.println("3. Delete Record");
            System.out.println("4. Edit Record");
            System.out.println("0. Exit");
            System.out.print("Enter Your Choice:");
            ch=sc.nextInt();
            switch(ch)
            {
                case 1:
                    System.out.println("insert");
                    break;
                case 2:
                    System.out.println("display");
                    break;
                case 3:
                    System.out.println("delete");
                    break;
                case 4:
                    System.out.println("edit");
                    break;
                case 0:
                    System.exit(0);
                default:
                    System.out.println("invalid");
            }
        }   
    }
}

回答by Durandal

Sorry, but this is impossible to do in a generic way.

抱歉,这是不可能以通用方式做到的。

The reason is, your program has no cluewhat the screenis. System.out can refer to anything, NUL-device, some kind of console or even a file. The console mayallow clearing, but how the console is to be cleared depends entirely on the type of the console (The usual console in an IDE can not be cleared, that would contradict its purpose of logging program output, while the console of a DOS-prompt/shell usually can be cleared).

原因是,您的程序不知道屏幕是什么。System.out 可以指任何东西,NUL 设备,某种控制台甚至文件。控制台可能允许清除,但是如何清除控制台完全取决于控制台的类型(IDE 中通常的控制台无法清除,这与其记录程序输出的目的相矛盾,而 DOS 的控制台-prompt/shell 通常可以清除)。

Your best bet would be to assume the console does support ANSI codes and send the ANSI control code for clear screen as suggested in the comments by MohdAdnan:

您最好的选择是假设控制台确实支持 ANSI 代码,并按照 MohdAdnan 的评论中的建议发送 ANSI 控制代码以清除屏幕:

System.out.print("\u001b[2J");
System.out.flush();