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[] args) throws Exception {String line;int count = 0;// file open using FileReaderFileReader file = new FileReader("D://data.txt");BufferedReader br = new BufferedReader(file);// Iterate file till end of linewhile ((line = br.readLine()) != null) {String words[] = line.split(" ");// words countcount = count + words.length;}System.out.println("The total words in given file: " + count);br.close();}}
No comments:
Post a Comment