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