zygimantus
5/26/2015 - 5:57 PM

Java progress bar in console examples

Java progress bar in console examples

/**
 * Prints progress in command line, but slows down the process itself.
 * Usage example: printProgBar(100 * i / total);
 * NOTE: first must be 100 
 * @param percent 
 * @see http://nakkaya.com/2009/11/08/command-line-progress-bar/
 */
public static void printProgBar(int percent) {
    StringBuilder bar = new StringBuilder("[");

    for (int i = 0; i < 50; i++) {
        if (i < (percent / 2)) {
            bar.append("=");
        } else if (i == (percent / 2)) {
            bar.append(">");
        } else {
            bar.append(" ");
        }
    }

    bar.append("]   ").append(percent).append("%     ");
    System.out.print("\r" + bar.toString());
}