import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import javax.swing.JFileChooser; import interfaces.*; /** * An input iterator for City objects from file input. * * @(#)TraversalInFile 22 March 2006 * @author Viera K. Proulx */ public class TraversalInFile implements Traversal{ /*-------------------------------------------------------- Member data *------------------------------------------------------*/ /** the input reader */ protected BufferedReader buffer; /** one line of input at a time */ protected String line; /** a city object to hold values */ protected City c; /** determines whether the general dialog has been closed */ protected boolean closed = true; /** determines whether new city data has been submitted */ protected boolean submitted = false; /*-------------------------------------------------------- Constructor *------------------------------------------------------*/ /** * Select a file from which the data will be read, make sure * it can be opened. */ public TraversalInFile() { /** build a file chooser and have the user choose a file, quitting this operation if the user cancelled the choice */ JFileChooser chooser = new JFileChooser("."); /** set the file extension to be .txt */ //chooser.setFileFilter(new FileView.ExtensionFileFilter("txt")); closed = false; /** see if file was selected - quit if user canceled */ if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION){ closed = true; return; } try{ buffer = new BufferedReader( new FileReader(chooser.getSelectedFile())); } catch(FileNotFoundException e){ System.out.println("File not found exception: " + e); closed = true; } /** read one line of input from the selected file */ /** and extract values for the first city */ getnext(); } /*-------------------------------------------------------- Methods to implement the Traversal interface *------------------------------------------------------*/ /** process next item, if available */ public void getnext() { if (!closed) try{ if (buffer.ready()){ line = new String(buffer.readLine()); if ((line == null)||(line == "")){ buffer.close(); closed = true; } else{ read(line); } } else{ buffer.close(); closed = true; } } catch(Exception e) { System.out.println("Error in reading line exception: " + e); closed = true; } } /** produce a traversal for the rest of this list */ public Traversal getRest(){ getnext(); return this; } /** return the most recent submission - if available */ public City getFirst() { if (!closed) return c; else{ closed = true; return null; } } /** verify that new submission is available */ public boolean isEmpty() { return (closed); } /** extract next City object from the input string */ public void read(String s){ try { c = new City(); c.fromStringData(line); } catch(java.text.ParseException e){ System.out.println(e.getMessage() + " parse exception in line\n" + line); c = null; closed = true; } } /** Self Test */ public static void main(String argv[]){ System.out.println("Examples of CitiesList-s:"); Traversal tr = new TraversalInFile(); while (!tr.isEmpty()){ System.out.println(tr.getFirst().toString()); tr = tr.getRest(); } System.out.println("End of the list."); } }