Gibt es eine einfache Möglichkeit, einen Rolling-Prozentsatz für einen Prozess in Java zu implementieren, der in der Konsole angezeigt werden soll? Ich habe einen prozentualen Datentyp (double), den ich während eines bestimmten Prozesses generiert habe. Kann ich ihn jedoch in das Konsolenfenster zwingen und aktualisieren lassen, anstatt nur eine neue Zeile für jede neue Aktualisierung auf den Prozentsatz zu drucken? Ich habe darüber nachgedacht, ein cls zu pushen und zu aktualisieren, weil ich in einer Windows-Umgebung arbeite, aber ich hatte gehofft, dass Java eine Art eingebauter Fähigkeit hatte. Alle Vorschläge sind willkommen! Vielen Dank!Konsolenbasierter Fortschritt in Java
Antwort
Sie können einen Wagenrücklauf drucken \r
den Cursor an den Anfang der Zeile zu setzen.
Beispiel:
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) {}
}
}
Ich glaube nicht, dass es eine eingebaute Fähigkeit zu tun, was Sie suchen.
Es gibt eine Bibliothek, die es tun wird (JLine).
Sehen Sie diese tutorial
ich ziemlich sicher bin, gibt es keine Möglichkeit, etwas zu ändern ist, dass die Konsole gedruckt wurde bereits da Java die Konsole (Standard out) hält ein Print zu sein.
Ich weiß nichts über Java selbst, aber Sie können Terminalsteuerungscodes verwenden, um Dinge wie den Cursor neu zu positionieren. Einige Details hier: http://www.termsys.demon.co.uk/vtansi.htm
Deaktivieren Sie die Konsole, indem Sie den os spezifischen Befehl ausführen und drucken Sie dann den neuen Prozentsatz
ich ein solches Paket in Java geschrieben sind.
Für diejenigen, die sich wundern: es unterstützt Mac Terminal auch. –
große lib! Danke – Genry
Ich verwende folgenden 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);
}
Wow! Vielen Dank! Für mich funktioniert es wirklich gut und besser, als markierte Antwort. –
Nur eine Anmerkung für andere, wenn Sie Ihre for-Schleife mit 'i = 0 'starten, erhalten Sie möglicherweise eine IllegalArgumentException von diesem Code. Ansonsten wirkt ein Charme. –
Das schien nicht richtig für mich zu funktionieren.Ich kopierte es genau, und anstatt der Fortschrittsbalken in der gleichen Zeile zu bleiben, druckte es neue Zeilen für jeden. – jeshbreadnan
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;
}
}
spät für die Partei, aber hier ist eine Antwort:
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;
}
Denken Sie daran, System.out.print()
statt System.out.println()
- 1. Monitor GZip Download Fortschritt in Java
- 2. Holen Sie sich den Fortschritt der Entpackdatei in Java
- 3. Java-Anzeige ändern den Fortschritt in der Befehlszeile
- 4. Wie mache ich einen Fortschritt mit einer Variablen in Java?
- 5. Download Fortschritt in HTML5
- 6. Charting-Fortschritt in JIRA
- 7. Fortschritt Dialog nicht nach (Fortschritt == 100) in webview
- 8. MobileInstallationInstall Fortschritt
- 9. Fortschritt Zeichenfolge in C Parsen
- 10. Indeterminierter Fortschritt in Android-Aktivität
- 11. Zeige Fortschritt auf UI
- 12. Fortschritt verstecken JSON Felder
- 13. Tracking-Fortschritt von HTTPWebRequest
- 14. Datei-Upload-Fortschritt
- 15. Kuchen: Build-Skript Fortschritt
- 16. Shrink Database Fortschritt Status
- 17. Windows 7 Taskbar Fortschritt
- 18. Backgroundworker meldet keinen Fortschritt
- 19. PHP S3 Upload Fortschritt
- 20. Fortschritt für DBCC SHRINKFILE
- 21. AFHTTPRequestOperation Fortschritt ist ungleich
- 22. AVAssetExportSession stoppt den Fortschritt
- 23. CSS3 Animation "Fortschritt" Rückruf
- 24. Flex Multipart Upload Fortschritt
- 25. android Download Fortschritt
- 26. SQL Server erhält Fortschritt
- 27. Avaudiolayer Wiedergabe Fortschritt
- 28. Fortschritt von dataOutputStream.readFully
- 29. Webpack Chunk Lade Fortschritt
- 30. Wie kommt man SQLite ‚VACUUM‘ Fortschritt
Für die Aufzeichnung eine eng verwandte ältere Frage zu verwenden: http://stackoverflow.com/questions/852665/command-line-progress-bar-in-java – Jonik