// CSE 143, summer 2012 // Counts the number of words in a large file. // Demonstrates the use of a Set collection. import java.io.*; import java.util.*; public class WordCount { public static void main(String[] args) throws FileNotFoundException { HashSet words = new HashSet(); long start = System.currentTimeMillis(); System.out.println("Reading file..."); Scanner input = new Scanner(new File("smallmoby.txt")); // store words so that we can print them later while(input.hasNext()) { String word = input.next(); if (!words.contains(word)) { words.add(word); } } long end = System.currentTimeMillis(); long elapsed = end - start; System.out.println("The file has " + words.size() + " words."); System.out.println("Took " + elapsed + " ms."); // print all of the unique words that are in the file for (String word : words) { System.out.println(word); } } }