What's the best way to determine the total number of words of a file in Java?

前端 未结 6 1289
一向
一向 2021-01-14 01:28

What is the best way to find the total number of words in a text file in Java? I\'m thinking Perl is the best on finding things such as this. If this is true then calling a

6条回答
  •  我在风中等你
    2021-01-14 02:13

    making some assumptions about what defines a 'word', one solution would be to open the file using a text stream reader and scan it, counting the number of non-contiguous whitespace characters, plus one for the end, e.g.

     this is some sample text
     this is some more sample text
    

    the text above would have 11 words in it, counted as 9 spaces and 1 newline and 1 end-of-file

提交回复
热议问题