控制台进度显示就地更新:Update Progress in place
Posted javartisan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了控制台进度显示就地更新:Update Progress in place相关的知识,希望对你有一定的参考价值。
控制台显示进度信息:
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;
/**
* Created on 2022-04-25
*/
public class InPlacePrint
public static void main(String[] args)
long total = 235;
long startTime = System.currentTimeMillis();
for (int i = 1; i <= 100; i++)
try
Thread.sleep(200);
// // printProgress(startTime, total, i);
// System.out.print("\\b\\b" + i + "\\\\b 退格(BS) ,将当前位置移到前一列");
// //System.out.print("\\r " + i); // 回车(CR) ,将当前位置移到本行开头
List<String> eqs = Collections.nCopies(i, "=");
List<String> blanks = Collections.nCopies(100 - i, "#");
String join = String.join("", eqs);
String blank = String.join("", blanks);
String progress = new StringBuilder()
.append("\\r")
.append("[")
.append(join)
.append(blank)
.append("]")
.append(" ")
.append(i)
.append("%")
.toString();
System.out.print(progress);
catch (InterruptedException e)
private static void printProgress(long startTime, long total, long current)
long eta = current == 0 ? 0 :
(total - current) * (System.currentTimeMillis() - startTime) / current;
String etaHms = current == 0 ? "N/A" :
String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(eta),
TimeUnit.MILLISECONDS.toMinutes(eta) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(eta) % TimeUnit.MINUTES.toSeconds(1));
StringBuilder string = new StringBuilder(140);
int percent = (int) (current * 100 / total);
string
.append('\\r') // 重点之处: \\r回到行首
.append(String.join("", Collections.nCopies(percent == 0 ? 2 : 2 - (int) (Math.log10(percent)), " ")))
.append(String.format(" %d%% [", percent))
.append(String.join("", Collections.nCopies(percent, "=")))
.append('>')
.append(String.join("", Collections.nCopies(100 - percent, " ")))
.append(']')
.append(String
.join("", Collections.nCopies((int) (Math.log10(total)) - (int) (Math.log10(current)), " ")))
.append(String.format(" %d/%d, ETA: %s", current, total, etaHms));
System.out.print(string);
以上是关于控制台进度显示就地更新:Update Progress in place的主要内容,如果未能解决你的问题,请参考以下文章
控制台进度显示就地更新:Update Progress in place
控制台进度显示就地更新:Update Progress in place
java 进度对话框。字体:https://stackoverflow.com/questions/12841803/best-way-to-show-a-loading-progress-indic