简体   繁体   English

输入文件并计算字符

[英]Input the file and count the characters

I am writing a code that calculates how many words in a file 我正在编写一个计算文件中有多少个单词的代码

And my problem is: when I input the file, there will have more word than the original file...For example, in the file, the content is abcdabcd, but when I run the code, the console shows Total no. 我的问题是:当我输入文件时,单词比原始文件要多。例如,在文件中,内容为abcdabcd,但是当我运行代码时,控制台显示Total no。 of letters: 194 字母数:194

I am using netbeans IDE and mac, when I click the blank space instead of directly open the file, I found there are many words in front of abcdabcd, I guess perhaps this is the reason... But I don't know how to fix this problem on my code [![enter image description here][1]][1] 我正在使用netbeans IDE和mac,当我单击空白而不是直接打开文件时,我发现abcdabcd前面有很多单词,我想也许这就是原因...但是我不知道该怎么做。解决此问题的代码[![在此处输入图片描述] [1]] [1]

Can anyone help me solve this problem? 谁能帮我解决这个问题?

Thanks! 谢谢!

The input file is encoded with rtf (Rich Text Format), despite it being saved with a .txt extension. 尽管输入文件以.txt扩展名保存,但仍使用rtf(富文本格式)进行编码。 This encoding is commonly used to do many things plain text cannot do, such as bold and italic etc. However, to accomplish this, the file is filled with all sorts of other things to accompany the normal text, in your case "abcdef". 此编码通常用于完成纯文本无法完成的许多工作,例如粗体和斜体等。但是,要实现此目的,文件中会填充各种其他内容以与普通文本一起出现,在您的情况下为“ abcdef”。 Of course, java reads all of this as if it were a plain text file, and ends up counting all of the rtf formatting as well. 当然,java会将所有这些读取为纯文本文件,并且最终还要计算所有rtf格式。

I assume you're using TextEdit, so look at this tutorial to see how to use only plain text, so all of the extra formatting is not included in the final file. 我假设您使用的是TextEdit,因此请看本教程以了解如何仅使用纯文本,因此最终文件中不包括所有其他格式。

Hope this helped :) 希望这有帮助:)

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

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