Answer
Count Words, Characters, and Lines in a File
Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class WordCountInFile {
public static void main(String[] args) {
BufferedReader reader = null;
// Initialize counters to 0
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
try {
// Creating BufferedReader object to read the file
reader = new BufferedReader(
new FileReader("C:\\SeleniumPractice\\Test.txt"));
// Reading the first line
String currentLine = reader.readLine();
while (currentLine != null) {
// Increment line count for each line read
lineCount++;
// Split line by spaces to get words
String[] words = currentLine.split(" ");
// Add word count for this line
wordCount = wordCount + words.length;
// Iterate each word to count characters
for (String word : words) {
charCount = charCount + word.length();
}
// Read next line
currentLine = reader.readLine();
}
System.out.println("Number Of Chars In A File : " + charCount);
System.out.println("Number Of Words In A File : " + wordCount);
System.out.println("Number Of Lines In A File : " + lineCount);
} catch (IOException e) {
e.printStackTrace();
} finally {
// Always close reader in finally block
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Sample Output (for a test file)
CODE
Number Of Chars In A File : 142
Number Of Words In A File : 28
Number Of Lines In A File : 5
Modern Version: try-with-resources (Java 7+)
Java
import java.io.*;
import java.nio.file.*;
// Auto-closes reader — no need for finally block
try (BufferedReader reader = new BufferedReader(new FileReader("Test.txt"))) {
int lineCount = 0, wordCount = 0, charCount = 0;
String line;
while ((line = reader.readLine()) != null) {
lineCount++;
String[] words = line.trim().split("\\s+");
wordCount += words.length;
for (String w : words) charCount += w.length();
}
System.out.println("Lines: " + lineCount);
System.out.println("Words: " + wordCount);
System.out.println("Chars: " + charCount);
}
Simplest: Using Files.readAllLines() (Java 8+)
Java
import java.nio.file.*;
import java.util.*;
Path path = Paths.get("Test.txt");
List<String> lines = Files.readAllLines(path);
long lineCount = lines.size();
long wordCount = lines.stream()
.flatMap(l -> Arrays.stream(l.trim().split("\\s+")))
.count();
long charCount = lines.stream()
.flatMap(l -> Arrays.stream(l.trim().split("\\s+")))
.mapToLong(String::length).sum();
System.out.println("Lines: " + lineCount);
System.out.println("Words: " + wordCount);
System.out.println("Chars: " + charCount);
File Reading Classes Comparison
| Class | Buffered | Best for |
|---|---|---|
FileReader | No | Simple char-by-char reading |
BufferedReader | Yes | Line-by-line reading (most common) |
Scanner | Yes | Tokenized reading with nextLine()/nextInt() |
Files.readAllLines() | Internal | Modern, all lines into List |
Files.lines() | Internal | Stream-based, large files |
Automation Testing Relevance
Java
// Read test data from a CSV file for data-driven testing
try (BufferedReader br = new BufferedReader(new FileReader("testdata.csv"))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
String username = data[0];
String password = data[1];
// run test with each username/password pair
performLogin(username, password);
}
}
