简体   繁体   English

我的字节数组长度不等于文件.txt文件大小

[英]My byte array length is not equal to the file .txt file size

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. 因此,基本上我想做的是使JProgressBar从0-100%变为100%是包含9999行单词的完全读取的.txt文件。

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. 我试图通过将分割成字节的巨大字符串存储到字节数组中并用字节数组的长度更新JPBar来做到这一点。

To my surprise, the JProgressBar stopped at 91%. 令我惊讶的是,JProgressBar停止在91%。 Later I decided to print out the values and realized that the file length is ~10000 larger than the byte array length. 后来我决定打印出这些值,并意识到文件长度比字节数组长度大〜10000。

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());

The extra file contents are most likely the other part of Windows-style line endings. 多余的文件内容很可能是Windows样式行尾的另一部分。 Standard line endings in files on Windows are \\r\\n , but your in-memory string contains only \\n . Windows上文件的标准行结尾为\\r\\n ,但您的内存中字符串仅包含\\n This would accumulate a difference of 1 character per line ending, and that times 9999 lines matches the difference you're reporting. 这将在每行结束处累积1个字符的差异,而9999行与您报告的差异相匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM