download - How to show percentage progress of a downloading file in java console(without UI)? -
my part of java code below.
while (status == downloading) { /* size buffer according how of file left download. */ byte buffer[]; if (size - downloaded > max_buffer_size) { buffer = new byte[max_buffer_size]; } else { buffer = new byte[size - downloaded]; } // read server buffer. int read = stream.read(buffer); if (read == -1){ system.out.println("file downloaded"); break; } // write buffer file. file.write(buffer, 0, read); downloaded += read; } /* change status complete if point reached because downloading has finished. */ if (status == downloading) { status = complete; }
i want show progress of downloading file percentage updating progress line in console. please help. thanks.
this simple "progress bar" concept.
basically uses \b
character backspace on characters printed console before printing out new progress bar...
public class textprogress { /** * @param args command line arguments */ public static void main(string[] args) { system.out.println(""); printprogress(0); try { (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("["); (int count = 0; count < dotcount; count++) { sb.append("."); } (int count = dotcount; count < 10; count++) { sb.append(" "); } sb.append("]"); (int count = 0; count < sb.length(); count++) { system.out.print("\b"); } system.out.print(sb.tostring()); } }
it won't work within ide's, java's text components don't support \b
character, must run terminal/console
Comments
Post a Comment