Java read file display progress bar implementation method _java

Source: Internet
Author: User

This feature is simple to use, with two classes: Progressmonitorinputstream (mainly the entire class) and Progressmonitor, which are in javax.swing
General idea, you want to first know the size of the entire file, and currently read the size of the file, to obtain the entire size of the file method

Copy Code code as follows:

Progressmonitorinputstream Monitor;
/**
* @param indicate which component this progress bar is attached to
* @param display the message on this progress bar
* @param the input stream that needs to be monitored
*/
Monitor = new Progressmonitorinputstream (null, "Loading", New FileInputStream ("filename path"));
int all = Monitor.available ()//size of the entire file
int in = Monitor.read (data);//per read file size

For example: Each time you read a line of Str=in.readline (), then Data=str.instr.getbytes () +1, +1, mainly to get the number of bytes in the line break, otherwise, the final progress can not reach 100%

Copy Code code as follows:

int readed=0;//for files that have been read
reader+=in;//Add read File size

Calculation progress:

Copy Code code as follows:

float process = (float) readed/all * 100;//Calculate percent

Window display:

Copy Code code as follows:

Progressmonitor.setnote ("archived" + process + "%");//Show on progress bar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.