Java中的命令行进度条

162

我有一个在命令行模式下运行的Java程序,我想显示一个进度条,显示作业完成的百分比。和unix系统下使用wget看到的同样类型的进度条一样。这可能吗?


我不知道你具体问题的答案,但你可以从查看这个问题中提到的库开始:https://dev59.com/oHRC5IYBdhLWcg3wAcbU - Jonik
谢谢。那里引用的库更多是关于命令行参数解析,而不是在控制台中显示。但还是谢谢。 - g andrieu
8
这是相关话题。楼主询问是否可能,而不是寻求图书馆建议(至少在当前编辑中)。 - Neil McGuigan
我尝试了这段代码 https://dev59.com/_3NA5IYBdhLWcg3wVcT6 并且成功运行了!(只能在终端中正确运行,不要在Eclipse中使用) - Wendel
18个回答

2
static String progressBar(int progressBarSize, long currentPosition, long startPositoin, long finishPosition) {
    String bar = "";
    int nPositions = progressBarSize;
    char pb = '░';
    char stat = '█';
    for (int p = 0; p < nPositions; p++) {
        bar += pb;
    }
    int ststus = (int) (100 * (currentPosition - startPositoin) / (finishPosition - startPositoin));
    int move = (nPositions * ststus) / 100;
    return "[" + bar.substring(0, move).replace(pb, stat) + ststus + "%" + bar.substring(move, bar.length()) + "]";
}

image


2

当我需要延迟工具以防止竞态条件时,我会使用“弹跳”进度条。

private void delay(long milliseconds) {
    String bar = "[--------------------]";
    String icon = "%";

    long startTime = new Date().getTime();
    boolean bouncePositive = true;
    int barPosition = 0;

    while((new Date().getTime() - startTime) < milliseconds) {
        if(barPosition < bar.length() && barPosition > 0) {
            String b1 = bar.substring(0, barPosition);
            String b2 = bar.substring(barPosition);
            System.out.print("\r Delaying: " + b1 + icon + b2);
            if(bouncePositive) barPosition++;
            else barPosition--;
        } if(barPosition == bar.length()) {
            barPosition--;
            bouncePositive = false;
        } if(barPosition == 0) {
            barPosition++;
            bouncePositive = true;
        }

        try { Thread.sleep(100); }
        catch (Exception e) {}
    }
    System.out.print("\n");
}

2

这可以通过Java Curses库实现。这里是我找到的资源。我自己没有使用过,也不知道它是否跨平台。


Curses可能对我需要的简单使用有些额外负担,但这确实是一条正确的道路。谢谢。 - g andrieu

1
public static void main(String[] argv) throws Exception{


    System.out.write("\r".getBytes());
    int percentage =10;
    while(percentage <= 100) {
        String temp =generateStars(percentage);
        System.out.write(temp.getBytes());
        System.out.print("\b\b\b");
        percentage = percentage+10;
        Thread.sleep(500);
    }
}

    public static String generateStars(int percentage)
    {
        int startsNum = percentage / 4;
        StringBuilder builder = new StringBuilder();
        while(startsNum >= 0)
        {
        builder.append("*");
        startsNum--;
        }
        builder.append(percentage+"%");
        return builder.toString();
    }

0

我创建了一个进度条,里面包含了你可能需要的一切。

我甚至对它进行了文档化

而且为了更快的使用,我还编译了它

我知道没有人必须这样做,但是我仍然看到人们在10年后遇到同样的问题!

这里有一个例子:

//...
  //For demo only!
  public static void main(String[]a){
    final ProgressBar progress=new ProgressBar(100);
    progress.printBar();
    for(int i=0;i<100;i++){
      progress.addValue();
      progress.printBar();
      try{
        java.lang.Thread.sleep(100);
      }catch(Exception e){}
    }
  }
//...

这不是为了宣传,制作这个是为了帮助人们不要浪费时间在自学编程课程上!


0
public class ConsoleApp {
    static String DisplayBar(int i)
    {
        StringBuilder sb = new StringBuilder();

        int x = i / 2;
        sb.append("|");
        for (int k = 0; k < 50; k++)
            sb.append(String.format("%s", ((x <= k) ? " " : "=")));
        sb.append("|");

        return sb.toString();
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i <= 100; i++)
        {
            Thread.sleep(200);
            System.out.printf("\r%s %s  Done", DisplayBar(i), i);
        }
    }
}

0
public class ProgressBar
{
    private int max;

    public ProgressBar(int max0) {
        max = max0;
        update(0);
    }

    public void update(int perc) {
        String toPrint = "|";
        for(int i = 0; i < max; i++) {
            if(i <= (perc + 1))
                toPrint += "=";
            else
                toPrint += " ";
        }

        if(perc >= max)
            Console.print("\r");
        else
            Console.print(toPrint + "|\r");
    }
}

0
public class Main {

    public static void main(String[] args) throws Exception {

        System.out.println("Loading : ");
            int count =1;
            for(int j=1;j<150;j++){

                System.out.print("\r");
                if(count==1){
                    System.out.print("/");
                    count++;
                }
                else if(count==2){
                    System.out.print("|");
                    count++;
                }
                else if(count==3){
                    System.out.print("-");
                    count++;
                }
                else if(count==4){
                    System.out.print("\\");
                    count++;
                }
                else if(count==5){
                    System.out.print("|");
                    count++;
                }
                else
                    count = 1;
            Thread.sleep(200);
        }

        }

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接