
import java.io.*;

public class ArrayIODemo
{
    public static void main(String[] args)
    {
        Species[] oneArray = new Species[2];
        oneArray[0] =
                    new Species("Calif. Condor", 27, 0.02);
        oneArray[1] =
                    new Species("Black Rhino", 100, 1.0);

        try
        {
            ObjectOutputStream outputStream =
                  new ObjectOutputStream(
                      new FileOutputStream("array.file"));
            outputStream.writeObject(oneArray);
            outputStream.close( );
        }
        catch(IOException e)
        {
            System.out.println(
                     "Error writing to file array.file.");
            System.exit(0);
        }

         System.out.println(
                   "Array sent to file array.file.");

        System.out.println(
              "Now let's reopen the file and echo the array.");
        Species[] anotherArray = new Species[2];


        try
        {
            ObjectInputStream inputStream =
                 new ObjectInputStream(
                      new FileInputStream("array.file"));
           anotherArray = (Species[])inputStream.readObject( );
           inputStream.close( );
        }
        catch(Exception e)
        {
            System.out.println(
                     "Error reading file array.file.");
            System.exit(0);
        }

        System.out.println("The following were read\n"
                         + "from the file array.file:");
        int i;
        for (i = 0; i < anotherArray.length; i++)
        {
            System.out.println(anotherArray[i]);
            System.out.println( );
        }
        System.out.println("End of program.");
    }
}
