// CSE 143, Autumn 2013 // This program counts the number of words in a large file. // It demonstrates the use of a Set collection. import java.io.*; import java.util.*; public class WordCount { public static void main(String[] args) throws FileNotFoundException { Set words = new HashSet(); long start = System.currentTimeMillis(); System.out.println("Reading file..."); Scanner input = new Scanner(new File("poem.txt")); while (input.hasNext()) { String word = input.next(); 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."); for(String word : words) { System.out.println(word); } } }