简体   繁体   English

冲突的人物数量

[英]Conflicting character counts

I'm trying to find the number of characters in a given text file. 我正在尝试查找给定文本文件中的字符数。

I've tried using both a scanner and a BufferedReader, but I get conflicting results. 我尝试过使用扫描仪和BufferedReader,但结果却相互矛盾。 With the use of a scanner I concatenate every line after I append a new line character. 通过使用扫描仪,我在添加新行字符后连接每一行。 Eg like this: 像这样:

    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 the true character count for my file, which is 5799 这将返回我的文件的真实字符数,即5799

Whereas when 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. 我得到5892。

I know using the lineScanner will be off by one if there is only one line, but for my text file I get the correct ouput. 我知道如果只有一行,使用lineScanner将会关闭,但对于我的文本文件,我得到了正确的输出。

Also in notepad++ the file length in bytes is 5892 but the character count without blanks is 5706. 另外在记事本++中,文件长度(以字节为单位)为5892,但没有空格的字符数为5706。

Your file may have lines terminated with \\r\\n rather than \\n . 您的文件可能以\\r\\n而不是\\n终止行。 That could cause your discrepancy. 这可能会导致您的差异。

You have to consider the newline/carriage returns character in a text file. 您必须考虑文本文件中的换行符/回车符。 This also counts as a character. 这也算作一个角色。

I would suggest using the BufferedReader as it will return more accurate results. 我建议使用BufferedReader,因为它会返回更准确的结果。

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

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