Conflicting Symbols

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.

+4
source share
2 answers

You should consider a newline / caret character in a text file. It is also considered a symbol.

I would suggest using BufferedReader as it will return more accurate results.

+1
source

Your file may contain lines with \r\n rather than \n . This may result in your non-compliance.

+2
source

Source: https://habr.com/ru/post/1379537/


All Articles