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

前端 未结 6 1236
一向
一向 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:25

    int count = 0;
    Scanner sc = new Scanner(new File("my-text-file.txt")); 
    while (sc.hasNext()) {
       ++count;
       sc.next();
    }
    

提交回复
热议问题