Java 如何清除时钟系统的控制台

Java 如何清除时钟系统的控制台,java,Java,我正在编写一个程序,运行一个类,该类实现了runnable。 我有它,所以时间以HH:MM:SS的格式每秒打印到屏幕上。 代码如下: public class LaunchCounter { public static void main(String[] args) { //Runs the CounterThread new CounterThread().start(); } } 这是柜台 public class CounterThread implement

我正在编写一个程序,运行一个类,该类实现了runnable。 我有它,所以时间以
HH:MM:SS
的格式每秒打印到屏幕上。 代码如下:

public class LaunchCounter
{

 public static void main(String[] args)
 {
    //Runs the CounterThread
    new CounterThread().start();
  }
}
这是柜台

public class CounterThread implements Runnable
{
 //Declare new thread
 private Thread thread;

public void start()
{
        thread = new Thread(this, "");
        thread.start();
}

@Override
public void run()
{   
    //Formatter used to display just time not date
    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

        //never ending forloop to display time
        for(int i = 1; i > 0; i++)
        {
            try
            {

                //Runtime.getRuntime().exec( "cmd /c cls" );
                //Sleep for 1 second after each loop
                Thread.sleep(1000);

                //new calender is created
                Calendar cal = Calendar.getInstance();
                System.out.println(dateFormat.format(cal.getTime()));
            }
            catch(Exception e1)
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
}
这个很好用。 我试图实现的是,等待一秒钟后,打印的行被清除,新时间被打印,依此类推。 因此,
12:00:01
becococomes
12:00:02
,而out不需要换行

我已经尝试了
System.out.print(“\b\b\b\b\b\b”)
Runtime.getRuntime().exec(“cmd/c cls”)但这只是将方块打印到控制台


如何实现这一点?

使用
Runtime.getRuntime().exec(“cls”)。请参阅霍尔格的文章,了解您无法清除控制台的原因

为了解决这个问题,我们必须调用命令行解释器 (cmd)并告诉它执行允许调用 内置命令。此外,我们必须直接连接其输出 通道到Java进程的输出通道,该通道从 Java 7,使用inheritIO():

现在,当Java进程连接到控制台时,即 从没有输出重定向的命令行启动,它将被清除 控制台


使用
Runtime.getRuntime().exec(“cls”)。请参阅霍尔格的文章,了解您无法清除控制台的原因

为了解决这个问题,我们必须调用命令行解释器 (cmd)并告诉它执行允许调用 内置命令。此外,我们必须直接连接其输出 通道到Java进程的输出通道,该通道从 Java 7,使用inheritIO():

现在,当Java进程连接到控制台时,即 从没有输出重定向的命令行启动,它将被清除 控制台


问题是你正在使用的终端。(我猜您正在IDE中使用终端。)如果您的输出终端没有进行完整的终端模拟,它将忽略\b字符或将其显示为不可打印字符

我在IntelliJ IDEA 16中测试了以下代码,并验证了内置IDEA终端忽略了\b。然后我在MacOS终端上测试了它,它按照您希望的方式工作

package test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CounterThread implements Runnable {
  //Declare new thread
  private Thread thread;

  public void start() {
    thread = new Thread(this, "");
    thread.start();
  }

  @Override
  public void run() {
    //Formatter used to display just time not date
    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    //never ending forloop to display time
    for (int i = 1; i > 0; i++) {
      try {

        //Runtime.getRuntime().exec( "cmd /c cls" );
        //Sleep for 1 second after each loop
        Thread.sleep(1000);

        //new calender is created
        Calendar cal = Calendar.getInstance();
        System.out.print("\b\b\b\b\b\b\b\b");
        System.out.print(dateFormat.format(cal.getTime()));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
  }

  public static void main(String[] args) throws Exception {
    //Runs the CounterThread
    new CounterThread().start();
    final Object monitor = new Object();
    synchronized (monitor) {
      monitor.wait();
    }
  }
}

问题是你正在使用的终端。(我猜您正在IDE中使用终端。)如果您的输出终端没有进行完整的终端模拟,它将忽略\b字符或将其显示为不可打印字符

我在IntelliJ IDEA 16中测试了以下代码,并验证了内置IDEA终端忽略了\b。然后我在MacOS终端上测试了它,它按照您希望的方式工作

package test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CounterThread implements Runnable {
  //Declare new thread
  private Thread thread;

  public void start() {
    thread = new Thread(this, "");
    thread.start();
  }

  @Override
  public void run() {
    //Formatter used to display just time not date
    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    //never ending forloop to display time
    for (int i = 1; i > 0; i++) {
      try {

        //Runtime.getRuntime().exec( "cmd /c cls" );
        //Sleep for 1 second after each loop
        Thread.sleep(1000);

        //new calender is created
        Calendar cal = Calendar.getInstance();
        System.out.print("\b\b\b\b\b\b\b\b");
        System.out.print(dateFormat.format(cal.getTime()));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
  }

  public static void main(String[] args) throws Exception {
    //Runs the CounterThread
    new CounterThread().start();
    final Object monitor = new Object();
    synchronized (monitor) {
      monitor.wait();
    }
  }
}

为了noob的利益,我建议我补充一点,Java中的“控制台”不是屏幕上的窗口。它只是字节流的源和汇。如果屏幕上有一个窗口,则该窗口由其他程序(即“终端仿真器”、“控制台窗口”、“外壳窗口”或您的IDE)控制,以及其他程序如何解释您的程序写入
系统的字节。out
取决于它是什么程序,它是如何配置的,它正在模拟什么老式的硬件终端等等)我使用了你提供的代码,仍然没有运气。我正在Windows8.1上使用EclipseMarsIDE。这在这个IDE上肯定是不可能的。它在您的IDE中不起作用,因为Eclipse中的控制台输出不是带有终端模拟的合适终端(解释像\b这样的特殊字符的东西)。尝试在适当的终端仿真器中执行该程序。在OSX上,终端应用是内置的;在Linux上,几乎所有运行的shell都将在终端仿真器中;在Windows上,您可以尝试许多终端模拟器。为了noob的好处,我想补充一点,Java中的“控制台”不是屏幕上的窗口。它只是字节流的源和汇。如果屏幕上有一个窗口,则该窗口由其他程序(即“终端仿真器”、“控制台窗口”、“外壳窗口”或您的IDE)控制,以及其他程序如何解释您的程序写入
系统的字节。out
取决于它是什么程序,它是如何配置的,它正在模拟什么老式的硬件终端等等)我使用了你提供的代码,仍然没有运气。我正在Windows8.1上使用EclipseMarsIDE。这在这个IDE上肯定是不可能的。它在您的IDE中不起作用,因为Eclipse中的控制台输出不是带有终端模拟的合适终端(解释像\b这样的特殊字符的东西)。尝试在适当的终端仿真器中执行该程序。在OSX上,终端应用是内置的;在Linux上,几乎所有运行的shell都将在终端仿真器中;在Windows上,您可以尝试许多终端模拟器。