使用Windows“ cls”清除屏幕Java控制台应用程序中的命令 [英] Clear screen with Windows "cls" command in Java console application

查看:163
本文介绍了使用Windows“ cls”清除屏幕Java控制台应用程序中的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个游戏,该游戏涉及在每回合之后都要清除屏幕以提高可读性。唯一的问题是我无法使用基于Windows命令提示符的 cls命令,并且它不支持ANSI转义字符。我在下一页上使用了Dyndrilliac的解决方案,但它导致IOException:

I am working on a game that involves clearing the screen after each turn for readability. The only problem is I cannot use the Windows command prompt-based "cls" command and it does not support ANSI escape characters. I used Dyndrilliac's solution on the following page but it resulted in an IOException:

Java:清除控制台

用 cmd \C cls替换 cls只会打开一个新的命令提示符,已清除并关闭它而不访问当前控制台。如何使通过Windows命令提示符运行的Java程序访问命令提示符的参数并使用它们清除其输出?

Replacing "cls" with "cmd \C cls" only opened a new command prompt, cleared it, and closed it without accessing the current console. How do I make a Java program running through Windows Command Prompt access the command prompt's arguments and use them to clear its output?

推荐答案

new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();

在这里解决: Java:清除控制台

我意识到这是一篇过时的文章,但是当我发现有以下问题的答案时我讨厌没关系,我懂了,否则就死了。希望它能对我有所帮助。

I realize this is an old post, but I hate when I find questions with responses of never mind i got it, or it just dies off. Hopefully it helps someone as it did for me.

请记住,它不能在Eclipse中运行,但可以在常规控制台中使用。如果您担心跨操作系统,请更进一步:

Keep in mind it won't work in eclipse, but will in the regular console. take it a step further with if you're worried about cross OS:

        final String os = System.getProperty("os.name");
        if (os.contains("Windows"))
            new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
        else
            Runtime.getRuntime().exec("clear");

这篇关于使用Windows“ cls”清除屏幕Java控制台应用程序中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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