The following code will help both my 211 and
314 students in terms of how to read data from a sequential file.
314 students
try
{
FileInputStream file_in = new
FileInputStream("data314");
DataInputStream data_in = new DataInputStream(file_in);
do
{
String record =
data_in.readUTF(); // UTF is for strings
CODE TO HANDLE THE STRING DATA
counter ++;
} while ( counter
<10);
REST OF
YOUR PROGRAM CODE
}
catch (IOException e )
{
HANDLE ERROR CONDITION like print
message
}
211 students
try
{
FileInputStream file_in = new
FileInputStream("data1");
DataInputStream data_in = new DataInputStream(file_in);
Char race, gender;
int age, weight;
int counter=0;
DECLARE ALL OTHER COUNTER YOU WILL NEED
do
{
race =
data_in.readChar();
gender =
data_in.readChar();
age =
data_in.readInt();
weight =
data_in.readInt();
CODE TO HANDLE THE data one record at a time
counter ++;
} while ( counter
<10);
REST OF
YOUR PROGRAM CODE
}
catch (IOException e )
{
HANDLE ERROR CONDITION like print
message
}