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

查看:15
本文介绍了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.

关键是 的区别. 转到新行的开头.但是 只是 回车 - 它返回到同一行的开头.

The key is the difference between and . goes to the start of a new line. But 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

"|========        |
"

在进度条的下一个刻度上,用更长的进度条覆盖同一行.(因为我们使用的是 ,所以我们保持在同一行)例如:

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

"|=========       |
"

你必须记住的事情是完成后,如果你只是打印

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

"done!
"

您可能仍然有一些来自在线进度条的垃圾.所以在你完成进度条后,一定要打印足够的空白以将其从行中删除.如:

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             |
"

希望有所帮助.

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

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