Why would fr_count and len be different in the code below?
FileReader fr = new FileReader(filename);
int c;
long fr_count = 0;
while ( -1 != (c = fr.read()) )
fr_count++;
long len = new File(filename).length();
I’ve used the code above on two files. Here are the results:
test.txt
FileReader: 263742
File.length: 265963
output.enc
FileReader: 146360
File.length: 212998
fr_countis the number of characters you read from the file.lenis the number of bytes in the file. They’re two very different things. E.g. some characters are represented in multiple bytes, and some encodings use a byte order mark. Both of these will make for differences between the number of characters and the number of bytes in a file.