help using scanner class to count chars in file
807588Apr 17 2009 — edited Apr 18 2009Hi all, I am learning Java right now and ran into a problem. I need to use the Scanner class to accurately count the number of chars in a file including the line feed chars 10&13.
This what I have and it currently reports a 201 byte file as having 194 chars:
File file = new File(plainFile);
Scanner inputFile = new Scanner(file);
numberOfChars = 0;
String line;
//count characters in file
while (inputFile.hasNextLine())
{
line = inputFile.nextLine();
numberOfChars += line.length();
}
I know there are other ways using BufferedReader, etc but I have to use Scanner class to do this.
Thanks in advance!