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
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:
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
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:
Number of words is : 41
Related Articles:
- Java 8 – Count and print number of lines and words in a text file
- Java 8 – Count and print number of repeated word occurrences in a text file
- Java 8 – Count and print number of repeated character occurrences in a String
- Java – Count and print number of words and lines in a text file
- Java – Count and print number of repeated word occurrences in a String
- Java – Count and print number of repeated character occurrences in a String
References:
- Files (Java Platform SE 8 ) (oracle.com)
- Paths (Java Platform SE 8 ) (oracle.com)
- Path (Java Platform SE 8 ) (oracle.com)
- Java – Count and print number of words and lines in a text file
Happy Coding !!
Happy Learning !!