Java Program to find the number of the words in the given text file

Java Program to find the number of the words in the given text file

Example:

Input:  data.txt -> Being code expert is the codnig expert side for student.
Output: The total words in given file: 10

Approach

Java


import java.io.BufferedReader;
import java.io.FileReader;

public class WordCountInFile {
    public static void main(String[] argsthrows Exception {
        String line;
        int count = 0;

        // file open using FileReader
        FileReader file = new FileReader("D://data.txt");
        BufferedReader br = new BufferedReader(file);

        // Iterate file till end of line
        while ((line = br.readLine()) != null) {
            String words[] = line.split(" ");
            // words count
            count = count + words.length;
        }

        System.out.println("The total words in given file: " + count);
        br.close();
    }
}


No comments:

Post a Comment