Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set progress bar value according to the unknown length of text file

I currently have a program which reads from a text file and then writes to a database after each line it reads, the size of the text file is undetermined, some days the file could be more or less lines than other days.

I already have a swing worker that executes my functions so my progress bar works but right now I just have setIndeterminate to true so the user knows something is being done just not the actual progress.

Is there a way I can increment the progress bar after each line is read, but have it not reach 100 too early or too late, preferably without reading the the text file entirely before hand. Thanks, Beef.

like image 302
Beef Avatar asked Dec 28 '25 02:12

Beef


2 Answers

I'd use File.length() to determine the file size. Then keep track of the number of bytes read to determine the progress.

like image 66
S.L. Barth Avatar answered Dec 30 '25 15:12

S.L. Barth


If the file size is known before you are starting to read it you can read it line-by-line and count the percentage after every line: count the number of bytes in each line and devide it by the total number of bytes (i.e. file.length()).

like image 41
AlexR Avatar answered Dec 30 '25 16:12

AlexR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!