Java 8 – Count and print number of lines and words in a text file

In this article, we will count and print number of lines and words in a text file i.e.;

Counting & printing from text file:

  • Number of lines
  • Number of words

Sample text file:

Already, in one of the previous article we have discussed about counting & printing number of lines & words using Java 1.7 version

1. Count & Print number of Lines in a text file

  • First, read file from source location using java.nio.file.Path & java.nio.file.Paths
  • Then, read lines one-by-one using java.nio.file.Files
  • Invoking count() method on static lines() method will return number of lines in the provided text file
  • Finally, print line count to the console

CountLinesInFileUsingJava8.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package in.bench.resources.count.lines.words;
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
 
public class CountLinesInFileUsingJava8 {
 
    public static void main(String[] args) {
 
        // local variables
        long lineCount = 0;
        Path path = null;
 
 
        // read file from root folder
        path = Paths.get("BRN.txt");
 
 
        try {
 
            // read file and count no. of lines
            lineCount = Files.lines(path).count();
        }
        catch (IOException ioex) {
 
            // handle exception
            ioex.printStackTrace();
        }
 
 
        // print to console
        System.out.println("Number of lines is : " + lineCount);
    }
}

Output:

1
Number of lines is : 4

2. Count & Print number of Words in a text file

  • First, read file from source location using java.nio.file.Path & java.nio.file.Paths
  • Then, read lines one-by-one using java.nio.file.Files and invoke parallel stream to process stream and count number of lines
  • Inside Stream.flatMap() method, pass lines one-by-one and split on the basis of space which returns Arrays of Stream
  • Invoking count() method on the above stream will return number of words in the provided text file
  • Finally, print word count to the console

CountWordsInFileUsingJava8.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package in.bench.resources.count.lines.words;
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
 
public class CountWordsInFileUsingJava8 {
 
    public static void main(String[] args) {
 
        // local variables
        long wordCount = 0;
        Path path = null;
 
 
        // read file from root folder
        path = Paths.get("BRN.txt");
 
 
        try {
 
            // read file and count no. of words
            wordCount = Files.lines(path)
                    .parallel()
                    .flatMap(line -> Arrays.stream(line.trim().split(" ")))
                    .count();
        }
        catch (IOException ioex) {
 
            // handle exception
            ioex.printStackTrace();
        }
 
 
        // print to console
        System.out.println("Number of words is : " + wordCount);
    }
}

Output:

1
Number of words is : 41

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – Count and print number of repeated word occurrences in a text file
Java 8 - How to split a String and Collect to any Collection ?