https://www.cs.indiana.edu/classes/c212-dgerman/fall2012/lectureSeventeen.html

-bash-4.2$ ls -ld WordCount.java
-rw-r--r-- 1 dgerman faculty 1518 Oct 17 14:15 WordCount.java
-bash-4.2$ ls -ld data.txt
-rw-r--r-- 1 dgerman faculty 21 Oct 17 14:04 data.txt
-bash-4.2$ cat data.txt
This
  is a
test

.
-bash-4.2$ wc data.txt
 5  5 21 data.txt
-bash-4.2$ javac WordCount.java
-bash-4.2$ java WordCount data.txt
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][...] 
1. ***(This)***
2. ***(  is a )***
3. ***(test)***
4. ***()***
5. ***(.)***
I see 5 lines in this file.
I see 5 words (token) in this file.
I see 12 non-blank characters in this file.
I see 21 all characters in this file.
-bash-4.2$ cat WordCount.java
import java.io.*;
import java.util.*;

class WordCount {
  public static void main(String[] args) throws Exception {
    Scanner a = new Scanner(new File(args[0]));
    System.out.println(a); // print the Scanner, it's possible, so: why not?
    int lines, // count the number of lines in the file
        words = 0, // count the number of words (tokens) in the file
        nonBlankChars = 0, // counts the number of non-blank characters
        allChars = 0; // counts all characters in file including space
    for (lines = 0; a.hasNextLine(); ) { // line by line
      String line = a.nextLine(); // get the line
      allChars += line.length(); // count the characters on the line
      lines = lines + 1; // count the line
      allChars += 1; // count the new lines
      System.out.println( lines + ". ***(" + line + ")***"); // some feedback
      Scanner b = new Scanner(line); // let's get the tokens out of this line, one by one
      while (b.hasNext()) { // while I see a token
        String word = b.next(); // get it
        words += 1; // cont it
        nonBlankChars += word.length(); // count its characters
      }
    }
    System.out.println("I see " + lines         + " lines in this file.");
    System.out.println("I see " + words         + " words (token) in this file.");
    System.out.println("I see " + nonBlankChars + " non-blank characters in this file.");
    System.out.println("I see " + allChars      + " all characters in this file.");
  }
}
-bash-4.2$