// Computer Science 121
// File reading example

import java.io.*;

/* This program is an example of reading from a file into an array and displaying the contents of the array on the screen. */
public class TestFiles
{
     public static void main (String [] args)
     {
          ListManager reader = new ListManager ();
          reader.readData ();
          reader.displayData ();
     } // main method
}// class TestFiles

// ListManager is a class that manages an array of integers that are read from a file.
class ListManager
{
     private int [] dataList;
     private int listSize;
     final int maxSize = 10;
 
     ListManager ()
     {
          dataList = new int [maxSize];
          listSize = 0;
     } // constructor
 
     /* This method reads integers from a file and stores them in an array.  The first line is read before entering the loop.  This is  changed into an integer and stored in the array.  A line is read also at the end of the loop.  If this comes back empty (null), the loop is terminated. */
     public void readData ()
     {
          String itemString;
          try
          {
               BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream ("infile.txt")));
               itemString = infile.readLine ();
               while ((itemString != null) && (listSize < maxSize))
               {
                    dataList [listSize] = Integer.parseInt (itemString);
                    listSize ++;
                    itemString = infile.readLine ();
               } // while
               infile.close ();
          } catch (IOException e) {}
          catch (NumberFormatException e) {}
     } // method readData
 
    public void displayData ()
     {
          for (int count = 0; count < listSize; count ++)
               System.out.print (dataList [count] + " ");
          System.out.println ();
     } // method displayData
} // class ListManager