I am trying to find the number of characters in a given text file.
I tried using both a scanner and BufferedReader, but I get conflicting results. With a scanner, I concatenate each line after adding a new line character. For instance. eg:
FileReader reader = new FileReader("sampleFile.txt"); Scanner lineScanner = new Scanner(reader); String totalLines = ""; while (lineScanner.hasNextLine()){ String line = lineScanner.nextLine()+'\n'; totalLines += line; } System.out.println("Count "+totalLines.length());
This returns a true character counter for my file, which is 5799
If I use:
BufferedReader reader = new BufferedReader(new FileReader("sample.txt")); int i; int count = 0; while ((i = in.read()) != -1) { count++; } System.out.println("Count "+count);
I get 5892.
I know that using lineScanner will be disabled by one if there is only one line, but for my text file I get the correct output.
Also in notepad ++, the file length in bytes is 5892, but the number of characters without spaces is 5706.
source share