Java中的命令行进度条 [英] Command line progress bar in Java

查看:532
本文介绍了Java中的命令行进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以程序模式运行的Java程序。
我想显示一个进度条,显示完成的工作的百分比。
同样的进度条,你会看到在unix下使用wget。
这是可能吗?

I have a Java program running in command line mode. I would like to display a progress bar, showing the percentage of job done. The same kind of progress bar you would see using wget under unix. Is this possible?

推荐答案

我已经实现了这种事情。它不是关于java,而是要发送到控制台的什么字符。

I have implemented this sort of thing before. Its not so much about java, but what characters to send to the console.

关键是 \\\
\r
\\\
转到新行的开头。但 \r 只是回车 - 它会回到同一行的开头。

The key is the difference between \n and \r. \n goes to the start of a new line. But \r is just carriage return - it goes back to the start of the same line.

所以要做的就是打印进度条,例如打印字符串

So the thing to do is to print your progress bar, for example, by printing the string

"|========        |\r"

在进度条的下一刻,用较长的条覆盖同一行。 (因为我们使用\r,我们保持在同一行)例如:

On the next tick of the progress bar, overwrite the same line with a longer bar. (because we are using \r, we stay on the same line) For example:

"|=========       |\r"

你必须记住要做什么,如果您只是打印

What you have to remember to do, is when done, if you then just print

"done!\n"

你可能还有一些垃圾从进度条上就行了。因此,完成进度条后,请务必打印足够的空格以将其从行中删除。例如:

You may still have some garbage from the progress bar on the line. So after you are done with the progress bar, be sure to print enough whitespace to remove it from the line. Such as:

"done             |\n"

希望有帮助。

这篇关于Java中的命令行进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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