package se1021.fileio.hw; import java.io.*; import java.util.*; import javax.swing.*; /* * This application demonstrates file I/O using text, binary, and object approaches */ public class RosterIOApp { List roster; // a collection of Students // main entry method public static void main(String[] args) { new RosterIOApp(); } // constructor - inits the app public RosterIOApp() { roster = new ArrayList(); // create the actual collection // add some students to initialize the collection roster.add( new Student("Ann", 123, 3.50) ); roster.add( new Student("Bob", 456, 2.80) ); roster.add( new Student("Carl", 789, 3.00) ); String filename = "roster.txt"; // without path spec, JVM assumes the file is in the project directory // ask the user what to do... String options[] = {"Open/Read a File", "Save/Write a File"}; String choice = (String)JOptionPane.showInputDialog(null, "Choices", "Select an operation", JOptionPane.QUESTION_MESSAGE, null, options, "Open Text File"); //TODO: Use JFileChooser to select a file // try reading or writing a file, based on the user's choice try { if( choice.equals("Open/Read a File") ) { // read a file roster.clear(); // erase the initial roster openTextRoster( filename ); // read a text file System.out.println("The roster list is as follows:"); for( Student s: roster ) { System.out.println(s.getName() + "," + s.getId() + "," + s.getGpa() ); } } else if( choice.equals("Save/Write a File") ) { // write a file saveTextRoster( filename ); // write a text file System.out.println("Save complete!"); } } catch( IOException e ) { // catch only IOExceptions during read or write System.out.println( "IO Error: " + e ); } catch( Exception e ) { // catch any and all other exceptions during read or write System.out.println( "General Error: " + e ); } } // read a text file and create a roster from it private void openTextRoster( String filename ) throws Exception { } // save the roster to a text file private void saveTextRoster(String filename) throws Exception { } // read a binary file and create a roster from it private void openBinaryRoster( String filename ) throws Exception { } // save the roster to a binary file private void saveBinaryRoster( String filename ) throws Exception { } // read an object file and create a roster from it private void openObjectRoster( String filename ) throws Exception { } // save the roster (serialize it) to a file private void saveObjectRoster( String filename ) throws Exception { } }