/* This program lets the user specify a text file for input and a file for output. All the words are read from the input file. Words are converted to lower case. An alphabetical list of all the words that were found, without repetition, is written to the output file, with one word per line. A word in this program is defined to be any sequence of letters. This class depends on the non-standard classes TextIO and TextReader. It does the same thing as WordList.java, but it much shorter because it uses a TreeSet to do the sorting. */ import java.io.*; import java.util.TreeSet; import java.util.Iterator; public class WordListWithTreeSet { public static void main(String[] args) { TreeSet words; // The words from the file will be stored in this. // Since this is a TreeSet, the words in it are // sorted into increasing order and there are // no duplicates. TextReader in; // A stream for reading from the input file. PrintWriter out; // A stream for writing to the output file. String inputFileName; // Input file name, specified by the user. String outputFileName; // Output file name, specified by the user. words = new TreeSet(); // Start with an empty set. /* Get the input file name from the user and try to create the input stream. If there is a FileNotFoundException, print a message and terminate the program. */ TextIO.put("Input file name? "); inputFileName = TextIO.getln().trim(); try { in = new TextReader(new FileReader(inputFileName)); } catch (FileNotFoundException e) { TextIO.putln("Can't find file \"" + inputFileName + "\"."); return; } /* Get the output file name from the user and try to create the output stream. If there is an IOException, print a message and terminate the program. */ TextIO.put("Output file name? "); outputFileName = TextIO.getln().trim(); try { out = new PrintWriter(new FileWriter(outputFileName)); } catch (IOException e) { TextIO.putln("Can't open file \"" + outputFileName + "\" for output."); TextIO.putln(e.toString()); return; } /* Read all the words from the input stream and insert them into the TreeSet. Reading from a TextReader can result in an error of type TextReader.Error. If one occurs, print an error message and terminate the program. */ try { while (true) { // Skip past and non-letters in the input stream. If an // end-of-stream has been reached, end the loop. Otherwise, // read a word and add it to the set. while ( ! in.eof() && ! Character.isLetter(in.peek()) ) in.getAnyChar(); if (in.eof()) break; words.add(in.getAlpha()); // Add the word to the TreeSet. // Has no effect if the word is // already there! } } catch (TextReader.Error e) { TextIO.putln("An error occured while reading from the input file."); TextIO.putln(e.toString()); return; } /* Write all the words from the list to the ouput stream. */ Iterator iter = words.iterator(); while (iter.hasNext()) out.println(iter.next()); /* Finish up by checking for an error on the output stream and printing either a warning message or a message that the words have been output to the output file. */ if (out.checkError() == true) { TextIO.putln("Some error occured while writing output."); TextIO.putln("Output might be incomplete or invalid."); } else { TextIO.putln( words.size() + " words from \"" + inputFileName + "\" output to \"" + outputFileName + "\"."); } } // end main() } // end class WordListWithTreeSet