如何在java控制台(没有UI)中显示下载文件的百分比进度?

我的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; } 

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

如果你对输出感到满意

正在下载文件… 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/

我从来没有用过这个,所以我不能保证。 如果不对,快速谷歌会有很多选择。

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

基本上,在打印出新的进度条之前,它使用\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字符,您必须从终端/控制台运行

 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; } } 

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

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

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