/* * Created on Apr 4, 2005 * */ package se1021.fileio.demo; import java.awt.Color; import java.io.*; import java.util.*; import javax.swing.*; /** * @author hornick * */ public class FileIOApp { List roster; // main entry method public static void main(String[] args) { new FileIOApp(); } // constructor - inits the app public FileIOApp() { roster = new ArrayList(); 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 try { saveTextRoster( filename ); } catch( Exception e ) { System.out.println( "Error: " + e ); } } // save the roster to a text file private void saveTextRoster(String filename) { // attach to a File object (file or directory) File fileObject = new File( filename ); PrintWriter pw = new PrintWriter(fileObject); for(Student s: roster ){ pw.println( s.getName() + " " + s.getId() + " " + s.getGpa() ); } pw.close(); } }