Home >>Java Programs >Java Program to find the number of the words in the given text file
In this example, we will create a java program to find the most repeated word present in given text file.
import java.io.BufferedReader;
import java.io.FileReader;
public class Main
{
public static void main(String[] args) throws Exception
{
String line;
int count = 0;
FileReader file = new FileReader("example.txt ");
BufferedReader br = new BufferedReader(file);
while((line = br.readLine()) != null)
{
String words[] = line.split("");
count = count + words.length;
}
System.out.println("Number of words present in given file: " + count);
br.close();
}
}