This question already has an answer here:
这个问题在这里已有答案:
- Command line progress bar in Java 11 answers
Java 11中的命令行进度条答案
Is there are easy way to implement a rolling percentage for a process in Java, to be displayed in the console? I have a percentage data type (double) I generated during a particular process, but can I force it to the console window and have it refresh, instead of just printing a new line for each new update to the percentage? I was thinking about pushing a cls and updating, because I'm working in a Windows environment, but I was hoping Java had some sort of built-in capability. All suggestions welcomed! Thanks!
是否有简单的方法来实现Java中进程的滚动百分比,以便在控制台中显示?我有一个在特定过程中生成的百分比数据类型(双),但是我可以强制它到控制台窗口并刷新它,而不是仅为每个新更新打印一个新行吗?我正在考虑推动cls和更新,因为我在Windows环境中工作,但我希望Java具有某种内置功能。欢迎所有建议!谢谢!
9 个解决方案
#1
52
You can print a carriage return \r
to put the cursor back to the beginning of line.
您可以打印回车符\ r \ n将光标放回行的开头。
Example:
public class ProgressDemo {
static void updateProgress(double progressPercentage) {
final int width = 50; // progress bar width in chars
System.out.print("\r[");
int i = 0;
for (; i <= (int)(progressPercentage*width); i++) {
System.out.print(".");
}
for (; i < width; i++) {
System.out.print(" ");
}
System.out.print("]");
}
public static void main(String[] args) {
try {
for (double progressPercentage = 0.0; progressPercentage < 1.0; progressPercentage += 0.01) {
updateProgress(progressPercentage);
Thread.sleep(20);
}
} catch (InterruptedException e) {}
}
}
#2
8
I use following code:
我使用以下代码:
public static void main(String[] args) {
long total = 235;
long startTime = System.currentTimeMillis();
for (int i = 1; i <= total; i = i + 3) {
try {
Thread.sleep(50);
printProgress(startTime, total, i);
} 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')
.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(current == 0 ? (int) (Math.log10(total)) : (int) (Math.log10(total)) - (int) (Math.log10(current)), " ")))
.append(String.format(" %d/%d, ETA: %s", current, total, etaHms));
System.out.print(string);
}
#4
6
I don't think there's a built-in capability to do what you're looking for.
我认为没有内置的功能可以满足您的需求。
There is a library that will do it (JLine).
有一个库(JLine)。
See this tutorial
请参阅本教程
#5
4
I'm quite sure there is no way to change anything that the console has already printed because Java considers the console (standard out) to be a PrintStream.
我很确定无法更改控制台已经打印的任何内容,因为Java认为控制台(标准输出)是PrintStream。
#6
2
Don't know about anything built in to java itself, but you can use terminal control codes to do things like reposition the cursor. Some details here: http://www.termsys.demon.co.uk/vtansi.htm
不了解java本身内置的任何内容,但您可以使用终端控制代码来执行重新定位游标等操作。这里有一些细节:http://www.termsys.demon.co.uk/vtansi.htm
#7
0
Clear the console by running the os specific command and then print the new percentage
通过运行os specific命令清除控制台,然后打印新的百分比
#8
0
import java.util.Random;
public class ConsoleProgress {
private static String CURSOR_STRING = "0%.......10%.......20%.......30%.......40%.......50%.......60%.......70%.......80%.......90%.....100%";
private static final double MAX_STEP = CURSOR_STRING.length() - 1;
private double max;
private double step;
private double cursor;
private double lastCursor;
public static void main(String[] args) throws InterruptedException {
// ---------------------------------------------------------------------------------
int max = new Random().nextInt(400) + 1;
// ---------------------------------------------------------------------------------
// Example of use :
// ---------------------------------------------------------------------------------
ConsoleProgress progress = new ConsoleProgress("Progress (" + max + ") : ", max);
for (int i = 1; i <= max; i++, progress.nextProgress()) {
Thread.sleep(3L); // a task with no prints
}
}
public ConsoleProgress(String title, int maxCounts) {
cursor = 0.;
max = maxCounts;
step = MAX_STEP / max;
System.out.print(title);
printCursor();
nextProgress();
}
public void nextProgress() {
printCursor();
cursor += step;
}
private void printCursor() {
int intCursor = (int) Math.round(cursor) + 1;
System.out.print(CURSOR_STRING.substring((int) lastCursor, intCursor));
if (lastCursor != intCursor && intCursor == CURSOR_STRING.length())
System.out.println(); // final print
lastCursor = intCursor;
}
}
#9
0
Late for the party, but here's an answer:
派对迟到了,但这是一个答案:
public static String getSingleLineProgress(double progress) {
String progressOutput = "Progress: |";
String padding = Strings.padEnd("", (int) Math.ceil(progress / 5), '=');
progressOutput += Strings.padEnd(padding, 0, ' ') + df.format(progress) + "%|\r";
if (progress == 100.0D) {
progressOutput += "\n";
}
return progressOutput;
}
Remember to use System.out.print()
instead of System.out.println()
记得使用System.out.print()而不是System.out.println()
#1
52
You can print a carriage return \r
to put the cursor back to the beginning of line.
您可以打印回车符\ r \ n将光标放回行的开头。
Example:
public class ProgressDemo {
static void updateProgress(double progressPercentage) {
final int width = 50; // progress bar width in chars
System.out.print("\r[");
int i = 0;
for (; i <= (int)(progressPercentage*width); i++) {
System.out.print(".");
}
for (; i < width; i++) {
System.out.print(" ");
}
System.out.print("]");
}
public static void main(String[] args) {
try {
for (double progressPercentage = 0.0; progressPercentage < 1.0; progressPercentage += 0.01) {
updateProgress(progressPercentage);
Thread.sleep(20);
}
} catch (InterruptedException e) {}
}
}
#2
8
I use following code:
我使用以下代码:
public static void main(String[] args) {
long total = 235;
long startTime = System.currentTimeMillis();
for (int i = 1; i <= total; i = i + 3) {
try {
Thread.sleep(50);
printProgress(startTime, total, i);
} 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')
.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(current == 0 ? (int) (Math.log10(total)) : (int) (Math.log10(total)) - (int) (Math.log10(current)), " ")))
.append(String.format(" %d/%d, ETA: %s", current, total, etaHms));
System.out.print(string);
}
#3
#4
6
I don't think there's a built-in capability to do what you're looking for.
我认为没有内置的功能可以满足您的需求。
There is a library that will do it (JLine).
有一个库(JLine)。
See this tutorial
请参阅本教程
#5
4
I'm quite sure there is no way to change anything that the console has already printed because Java considers the console (standard out) to be a PrintStream.
我很确定无法更改控制台已经打印的任何内容,因为Java认为控制台(标准输出)是PrintStream。
#6
2
Don't know about anything built in to java itself, but you can use terminal control codes to do things like reposition the cursor. Some details here: http://www.termsys.demon.co.uk/vtansi.htm
不了解java本身内置的任何内容,但您可以使用终端控制代码来执行重新定位游标等操作。这里有一些细节:http://www.termsys.demon.co.uk/vtansi.htm
#7
0
Clear the console by running the os specific command and then print the new percentage
通过运行os specific命令清除控制台,然后打印新的百分比
#8
0
import java.util.Random;
public class ConsoleProgress {
private static String CURSOR_STRING = "0%.......10%.......20%.......30%.......40%.......50%.......60%.......70%.......80%.......90%.....100%";
private static final double MAX_STEP = CURSOR_STRING.length() - 1;
private double max;
private double step;
private double cursor;
private double lastCursor;
public static void main(String[] args) throws InterruptedException {
// ---------------------------------------------------------------------------------
int max = new Random().nextInt(400) + 1;
// ---------------------------------------------------------------------------------
// Example of use :
// ---------------------------------------------------------------------------------
ConsoleProgress progress = new ConsoleProgress("Progress (" + max + ") : ", max);
for (int i = 1; i <= max; i++, progress.nextProgress()) {
Thread.sleep(3L); // a task with no prints
}
}
public ConsoleProgress(String title, int maxCounts) {
cursor = 0.;
max = maxCounts;
step = MAX_STEP / max;
System.out.print(title);
printCursor();
nextProgress();
}
public void nextProgress() {
printCursor();
cursor += step;
}
private void printCursor() {
int intCursor = (int) Math.round(cursor) + 1;
System.out.print(CURSOR_STRING.substring((int) lastCursor, intCursor));
if (lastCursor != intCursor && intCursor == CURSOR_STRING.length())
System.out.println(); // final print
lastCursor = intCursor;
}
}
#9
0
Late for the party, but here's an answer:
派对迟到了,但这是一个答案:
public static String getSingleLineProgress(double progress) {
String progressOutput = "Progress: |";
String padding = Strings.padEnd("", (int) Math.ceil(progress / 5), '=');
progressOutput += Strings.padEnd(padding, 0, ' ') + df.format(progress) + "%|\r";
if (progress == 100.0D) {
progressOutput += "\n";
}
return progressOutput;
}
Remember to use System.out.print()
instead of System.out.println()
记得使用System.out.print()而不是System.out.println()