Computer Science Example
Reading from a file into an array of objects and then saving the contents to a second file.

import java.io.*;
import java.text.*;

public class ArrayOfObjects
{
     public static void main (String [] args)
     {
          Roster roster = new Roster ();
          roster.readList ();
          roster.displayList ();
          roster.saveList ();
    }
} // ArrayOfObjects

class Student
{
     String id, name;
     double qpa;
 
     Student (String ID)
     {
          id = ID;
          name = "";
          qpa = 0;
     } // constructor
 
     public void readData (BufferedReader infile) throws IOException
     {
          try
          {
               name = infile.readLine ();
               qpa = Double.parseDouble (infile.readLine ());
          } catch (NumberFormatException e)
          {  System.out.println ("Number Format Exception in the qpa for " + name); }
     } // readData
 
     public void displayData ()
     {
          System.out.println ("ID: " + id);
          System.out.println ("Name: " + name);
          System.out.println ("QPA: " + decimals (qpa));
          System.out.println ();
     } // displayData
 
     public void saveData (PrintWriter outfile)
     {
          outfile.println ("ID: " + id);
          outfile.println ("Name: " + name);
          outfile.println ("QPA: " + decimals (qpa));
          outfile.println ();
     } // saveData
 
     private String decimals (double num)
     {
          DecimalFormat decFor = new DecimalFormat ();
          decFor.setMaximumFractionDigits (2);
          decFor.setMinimumFractionDigits (2);
          return decFor.format (num);
     } // decimals
} // Student

class Roster
{
     final int maxSize = 10;
     Student [] list = new Student [maxSize];
     int listSize = 0;
 
     public void readList ()
     {
          try
          {
               BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream ("students.txt")));
               String id = infile.readLine ();
               while ((id != null) && (listSize < maxSize))
               {
                    Student student = new Student (id);
                    student.readData (infile);
                    list [listSize] = student;
                    listSize ++;
                    id = infile.readLine ();
               }
               infile.close ();
          } catch (IOException e) {System.out.println ("File not found");}
     } // readList
 
     public void displayList ()
     {
          for (int count = 0; count < listSize; count ++)
          {
               System.out.println ();
               list [count].displayData ();
          }
     } // displayList
 
     public void saveList ()
     {
          try
          {
               PrintWriter outfile = new PrintWriter (new FileOutputStream ("outfile.txt"));
               for (int count = 0; count < listSize; count ++)
               {
                    outfile.println ();
                    list [count].saveData (outfile);
               }
               outfile.close ();
         } catch (IOException e) {System.out.println ("File not Found");}
     } // saveList
} // Roster