Reputation: 45
So basically what I am trying to do is to make a JProgressBar go from 0-100% where 100% is a fully read .txt file that contains 9999 lines of words.
I am trying to do this by storing a huge string segmented into bytes into a byte array and updating the JPBar with the length of the byte array.
To my surprise, the JProgressBar stopped at 91%. Later I decided to print out the values and realized that the file length is ~10000 larger than the byte array length.
Could someone explain to me why this is the case and how can I potentially get it right? I realize that I am most likely missing a concept about reading and counting characters. The code snippet is below.
Thanks!
bar.setMinimum(0);
bar.setMaximum((int)file.length());
try{
while((check = reader.readLine()) != null){
words = words + check + "\n";
stringCount = words.getBytes();
bar.setValue(stringCount.length);
}
}catch(Exception e){}
System.out.println(stringCount.length);
System.out.println(file.length());
Upvotes: 1
Views: 2234
Reputation: 5087
The extra file contents are most likely the other part of Windows-style line endings. Standard line endings in files on Windows are \r\n
, but your in-memory string contains only \n
. This would accumulate a difference of 1 character per line ending, and that times 9999 lines matches the difference you're reporting.
Upvotes: 2