Monday 22 July 2013

Write a program to find number of words in a file

 Write a program to find number of words in a file

Write a program to find number of words in a file

The below program is to find number of words in a file,number of words in a file,find the number of words in a file
Its the program to find a number of words in a file
//Write a program to find number of words in a file (file should be from commandline)
package com.naga.programs;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class CountWordsInFile {

    public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
       
            FileInputStream fis = new FileInputStream("D:/Practice/file.txt");
            DataInputStream in = new DataInputStream(fis);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strline=null;
            int count=0;
            while((strline = br.readLine()) != null){
                String[] m = strline.split(" ");
                for(int i=0;i<m.length;i++){
                    if(m[i] != null){
                        count++;
                    }
                }
            }
            System.out.println("The number of words in a file: "+count);
    }

}