2

我的java代码部分如下。

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;

        }

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

4

4 回答 4

3

这是一个简单的“进度条”概念。

基本上,\b在打印出新的进度条之前,它使用该字符来退格先前打印到控制台的字符...

public class TextProgress {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("");
        printProgress(0);
        try {
            for (int index = 0; index < 101; index++) {
                printProgress(index);
                Thread.sleep(125);
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(TextProgress.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void printProgress(int index) {

        int dotCount = (int) (index / 10f);
        StringBuilder sb = new StringBuilder("[");
        for (int count = 0; count < dotCount; count++) {
            sb.append(".");
        }
        for (int count = dotCount; count < 10; count++) {
            sb.append(" ");
        }
        sb.append("]");
        for (int count = 0; count < sb.length(); count++) {
            System.out.print("\b");
        }
        System.out.print(sb.toString());

    }
}

它不适用于大多数 IDE,因为 Java 的文本组件不支持该\b字符,您必须从终端/控制台运行

于 2013-04-06T03:44:38.950 回答
3

如果您对输出感到满意,例如

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

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

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("\b");
  }
  String is = String.toString(i);
  System.out.print(is);
  lastSize = is.length();
}

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

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

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

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

于 2013-04-05T21:05:32.557 回答
0
public class ConsoleProgressCursor {

    private static String CURSOR_STRING = "0%.......10%.......20%.......30%.......40%.......50%.......60%.......70%.......80%.......90%.....100%";

    private double max = 0.;
    private double cursor = 1.;
    private double lastCursor = 0.;

    public static void main(String[] args) throws InterruptedException {
        final int max = 100;
        ConsoleProgressCursor progress = new ConsoleProgressCursor("Progress : ", max);
        for (int i = 0; i < max; i++) {
            Thread.sleep(10L);
            progress.nextProgress();
        }
        progress.endProgress();
    }

    public ConsoleProgressCursor(String title, double maxCounts) {
        max = maxCounts;
        System.out.print(title);
    }

    public void nextProgress() {
        cursor += 100. / max;
        printCursor();
    }

    public void nextProgress(double progress) {
        cursor += progress * 100. / max;
        printCursor();
    }

    public void endProgress() {
        System.out.println(CURSOR_STRING.substring((int) lastCursor, 101));
    }

    private void printCursor() {
        final int intCursor = (int) Math.floor(cursor);
        System.out.print(CURSOR_STRING.substring((int) lastCursor, intCursor));
        lastCursor = cursor;
    }
}
于 2017-03-18T20:48:19.383 回答
-1

首先,您无法在纯控制台上模拟真实的进度条。

@Jeffrey Blattman 提供了一个简单输出到控制台的解决方案以及提供高级控制台工作的库

如果您对在控制台中构建 UI 更感兴趣,您可以尝试 Lanterna ( http://code.google.com/p/lanterna/ ) - 这是非常好的东西,特别是如果您希望为老派部门开发软件,例如图书馆或政府会计师

于 2013-04-05T23:26:18.347 回答