如何在 Java 控制台(无 UI)中显示下载文件的百分比进度?

How to show percentage progress of a downloading file in java console(without UI)?(如何在 Java 控制台(无 UI)中显示下载文件的百分比进度?)
本文介绍了如何在 Java 控制台(无 UI)中显示下载文件的百分比进度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的部分java代码如下.

My part of java code is below.

while (status == DOWNLOADING) {
    /* Size buffer according to how much of the
       file is left to download. */
            byte buffer[];
            if (size - downloaded > MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[size - downloaded];
            }

            // Read from server into buffer.
            int read = stream.read(buffer);
            if (read == -1){
                System.out.println("File was downloaded");
                break;
            }

            // Write buffer to file.
            file.write(buffer, 0, read);
            downloaded += read;

        }

  /* Change status to complete if this point was
     reached because downloading has finished. */
        if (status == DOWNLOADING) {
            status = COMPLETE;

        }

我想通过更新控制台中的进度行以百分比形式显示下载文件的进度.请帮忙.谢谢.

I want to show the progress of the downloading file as percentage by updating the progress line in console. Please help. Thanks.

推荐答案

如果你对输出满意的话

正在下载文件 ... 10% ... 20% ... 38% ...

Downloading file ... 10% ... 20% ... 38% ...

不断追加到该行,你可以只 print 而不是 println.如果您想做一些有限的事情,比如只更新几个字符,您可以打印退格字符以擦除然后重新打印百分比,例如:

that keeps appending to the line, you can just print instead of println. If you want to do something limited like just update a few characters in place, you can print the backspace character to erase then re-print the percent, for example:

System.out.print("index at: X");
int lastSize = 1;
for (int i = 0; i < 100; i++) {
  for (int j = 0; j < lastSize; j++) {
    System.out.print("");
  }
  String is = String.toString(i);
  System.out.print(is);
  lastSize = is.length();
}

注意代码如何跟踪打印了多少个字符,因此它知道要打印多少个退格键来擦除附加的文本.

Note how the code tracks how many characters were printed, so it knows how many backspaces to print to erase the appended text.

比这更复杂的是,您需要某种控制台或终端控制 SDK.ncurses 是 Unix 标准,看起来有一个 Java 端口:

Something more complex than that, you need some sort of console or terminal control SDK. ncurses is the Unix standard, and it looks like there's a Java port:

http://sourceforge.net/projects/javacurses/

我从来没有使用过这个,所以我不能保证它.如果它不正确,快速谷歌显示了许多替代方案.

I have never used this one so I can't vouch for it. If it's not right, a quick Google showed many alternatives.

这篇关于如何在 Java 控制台(无 UI)中显示下载文件的百分比进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)