TShopping

 找回密碼
 註冊
搜索
查看: 1833|回復: 0
打印 上一主題 下一主題

[教學] Java實現在linux終端顯示的字符進度條

[複製鏈接]
跳轉到指定樓層
1#
發表於 2014-8-13 17:52:15 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook


前言
面向用戶使用的程序都會考慮響應性,如:上傳、下載文件會顯示已經完成百分之多少,方便用戶瞭解處理的進度。在Swing和AWT編寫的應用有現成的進度條控件可用,但對於非界面程序就需要自己實現了。
花了點時間寫了一個類似於wget的字符進度條,可用於在Linux的命令終端和Windows的命令窗口中實時顯示任務處理的進度。

原理:
在每次顯示進度條時將光標定位回當前行的最左邊,輸出當前的進度條覆蓋舊的進度條。

特點:

在一行中實時顯示進度和百分比,類似於wget的進度條

已知存在的問題:
1、在Eclipse的控制台顯示不正常,每次刷新進度條時會換行。
2、當進度條的長度超過控制終端的顯示區域時,每次刷新進度條時會換行。

進度條代碼
  1. package cn.aofeng.util;
  2. import java.text.DecimalFormat;
  3. /**
  4. * 控制台字符型進度條。
  5. *
  6. * @author 傲風 <<a href="mailto:aofengblog@163.com" target="_blank">aofengblog@163.com</a>>
  7. */
  8. public class ConsoleProgressBar {
  9.     private long minimum = 0; // 進度條起始值
  10.     private long maximum = 100; // 進度條最大值
  11.     private long barLen = 100; // 進度條長度
  12.     private char showChar = '='; // 用於進度條顯示的字符
  13.     private DecimalFormat formater = new DecimalFormat("#.##%");
  14.     /**
  15.      * 使用系統標準輸出,顯示字符進度條及其百分比。
  16.      */
  17.     public ConsoleProgressBar() {
  18.     }
  19.     /**
  20.      * 使用系統標準輸出,顯示字符進度條及其百分比。
  21.      *
  22.      * @param minimum 進度條起始值
  23.      * @param maximum 進度條最大值
  24.      * @param barLen 進度條長度
  25.      */
  26.     public ConsoleProgressBar(long minimum, long maximum,
  27.             long barLen) {
  28.         this(minimum, maximum, barLen, '=');
  29.     }
  30.     /**
  31.      * 使用系統標準輸出,顯示字符進度條及其百分比。
  32.      *
  33.      * @param minimum 進度條起始值
  34.      * @param maximum 進度條最大值
  35.      * @param barLen 進度條長度
  36.      * @param showChar 用於進度條顯示的字符
  37.      */
  38.     public ConsoleProgressBar(long minimum, long maximum,
  39.             long barLen, char showChar) {
  40.         this.minimum = minimum;
  41.         this.maximum = maximum;
  42.         this.barLen = barLen;
  43.         this.showChar = showChar;
  44.     }
  45.     /**
  46.      * 顯示進度條。
  47.      *
  48.      * @param value 當前進度。進度必須大於或等於起始點且小於等於結束點(start <= current <= end)。
  49.      */
  50.     public void show(long value) {
  51.         if (value < minimum || value > maximum) {
  52.             return;
  53.         }
  54.         reset();
  55.         minimum = value;
  56.         float rate = (float) (minimum*1.0 / maximum);
  57.         long len = (long) (rate * barLen);
  58.         draw(len, rate);
  59.         if (minimum == maximum) {
  60.             afterComplete();
  61.         }
  62.     }

  63.     private void draw(long len, float rate) {
  64.         for (int i = 0; i < len; i++) {
  65.             System.out.print(showChar);
  66.         }
  67.         System.out.print(' ');
  68.         System.out.print(format(rate));
  69.     }
  70.    
  71.     private void reset() {
  72.         System.out.print('\r');
  73.     }
  74.    
  75.     private void afterComplete() {
  76.         System.out.print('\n');
  77.     }
  78.    
  79.     private String format(float num) {
  80.         return formater.format(num);
  81.     }
  82.    
  83.     public static void main(String[] args) throws InterruptedException {
  84.         ConsoleProgressBar cpb = new ConsoleProgressBar(0, 100, 20, '=');
  85.         for (int i = 1; i <= 100; i++) {
  86.             cpb.show(i);
  87.             Thread.sleep(100);
  88.         }
  89.     }

  90. }
複製代碼
效果
java -cp ./classes cn.aofeng.util.ConsoleProgressBar
完成百分之30時顯示:======= 30%
完成百分之50時顯示:========== 50%
完成百分之100時顯示:==================== 100%


<正文結束>




 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-24 16:37 , Processed in 0.054823 second(s), 24 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表