JB Header
Java 8 - How to count words in a text file in Java using NIO and Streams API
This quick code tip shows how to count number of words in a text file in Java 8 using the Streams API along with the NIO API. Do take a note of how the file contents are read and the words counted using a single line of pipelined instructions with the Java 8 Streams API.

Contents of the input Text file "WordCount.txt" (Total: 30 words)
This is the first paragraph. This is the second line. This is the third line.
This is the second paragraph. This is the fifth line. This is the sixth line.

Java 8 Streams code to count the number of words in a text file
package com.javabrahman.java8;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;
public class FileWordCount {
  public static void main(String args[]) {
    long wordCount = 0;
    Path textFilePath = Paths.get("C:\\JavaBrahman\\WordCount.txt");
    try {
      Stream<String> fileLines = Files.lines(textFilePath, Charset.defaultCharset());
      wordCount = fileLines.flatMap(line -> Arrays.stream(line.split(" "))).count();
    } catch (IOException ioException) {
      ioException.printStackTrace();
    }
    System.out.println("Number of words in WordCount.txt: "+ wordCount);
  }
}
 OUTPUT of the above code
Number of words in WordCount.txt: 30
Explanation of the code
  • The get() method of NIO API's java.nio.file.Paths class is used to get the java.nio.file.Path handle, named textFilePath, to the text file "WordCount.txt". The full URI to the text file is passed as input to the method.
  • lines() method of java.nio.file.Files class is invoked with the Path variable textFilePath and the deault character set using Charset.defaultCharset() method. The defaultCharset() method returns the default character set of the JVM on which the code is running.
  • Files.lines() method returns a Stream of Strings, or Stream<String>, with each String element of the Stream representing a line in the file.
  • Next we map each line of the text file, of type String, to its equivalent flattened Stream of word strings. This is done in 3 steps-
    1. String.split() method is specified using a lambda expressionClick to read Java 8 lambda expressions tutorial to separate the words in each line and store them in a String array.
    2. Arrays.stream() method then creates a Stream of Strings, or Stream<String> for every line.
    3. We now effectively have a Stream of Stream<String> elements or Stream<Stream<String>>. We then apply Stream.flatMap() method to flattenClick to understand the working of flatMap() method the Stream<Stream<String>> into one Stream<String>. The elements of this flattened stream are all the words in the text file.
  • The flattened stream is then pipelinedClick to understand the concept of pipelining to Stream.count() method is used to count the number of elements in the Stream<String> which is the total number of words in the text file or 30, as is the output.