Search Java Code Snippets

  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.

#Java - Code Snippets for '#Count number of words' - 1 code snippet(s) found

 Sample 1. Write a Program to read a file and then count number of words

public class CountwordsinFile {
public static void main(String[] args) throws IOException{
String line = null;

File file = new File("C:Hello.txt");
FileReader fileReader = new FileReader(file);

BufferedReader bufferedReader = new BufferedReader(fileReader);

int countWords = 0;

do {

line = bufferedReader.readLine();
countWords += line.split(" ").length;

} while(line != null);

   Like      Feedback     file handling  count number of words  code  coding  file handling

Subscribe to Java News and Posts. Get latest updates and posts on Java from
Enter your email address:
Delivered by FeedBurner