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 uses a StreamTokenizer to read
integers from a text file called infile.txt. The StreamTokenizer
skips white space (spaces, tabs and end of line characters) but gets all
other strings. */
public void readData ()
{
try
{
BufferedReader infile = new BufferedReader (new InputStreamReader (new
FileInputStream ("infile.txt")));
StreamTokenizer tokens = new StreamTokenizer (infile);
int next = tokens.nextToken ();
while (next != tokens.TT_EOF)
{
dataList [listSize] = (int) tokens.nval;
listSize ++;
next = tokens.nextToken ();
} // 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