import java.io.EOFException; // For when we hit end of file import java.io.FileNotFoundException; // Dealing with files import java.io.IOException; // Misc. I/O errors import rebelsky.util.IncomparableException; // Required by quickSort import rebelsky.util.ComparableString; // Comparing strings import rebelsky.util.EmptyListException;// Required for dealing with lists import rebelsky.io.SimpleReader; // Reading input import rebelsky.io.SimpleOutput; // Writing output import NodeList; // Does the work /** * Sort the lines of a file specified on the command line. * * @author Samuel A. Rebelsky * @version 1.0 of March 1998a */ public class LineSorter { /** * Sort the lines of a file specified on the command line. */ public static void main(String[] args) { // Input and output SimpleOutput out = new SimpleOutput(); // The input will be changed to a file, but we initialize it // to something so that Java doesn't complain. SimpleReader in = new SimpleReader(); // A list of the lines of the file NodeList lines = new NodeList(); // Ensure that there is exactly one file specified on the // command line. if (args.length != 1) { out.println("You must specify a file to sort."); System.exit(1); } // Open the input file try { in = new SimpleReader(args[0]); } catch (FileNotFoundException fnfe) { out.println("Couldn't find file '" + args[0] + "'"); System.exit(2); } // Read the lines try { try { while (true) { // Read a line, make it comparable, shove it in a node, and // add to the end of the list. lines.addToEnd(new Node(new ComparableString(in.readLine()))); } } catch (EOFException ee) { // We're done with the input file. Close it. in.close(); } } catch (IOException ie) { out.println("Agh. Some weird IO problem."); System.exit(3); } // Sort the lines try { lines = lines.quickSort(); } catch (IncomparableException ie) { out.println("Failed to sort."); System.exit(4); } // And print them out try { while (!lines.empty()) { out.println(lines.first().toString()); lines.deleteFirst(); } // while } catch (EmptyListException ele) { // Do nothing, it's over } } // main(String[]) } // LineSorter