/**
* Sets length of RAF file to fileLength, and creates "null" students in
* all records
*/
{
try
{
raf.setLength(fileLength);
tempStudent = nullStudent();
for (int i = 1; i < numOfRecords; i++)
{
writeRecord(tempStudent, i);
}
}
{
throw e;
}
}
/**
* Returns "null" student
*/
{
int id = 0;
boolean gender = false;
int locker = 0;
int yearGrade = 0;
int graduationYear = 0;
//creates "null" student
tempStudent = new Student(username, homeroom, id, gender,
birthdate, birthplace, locker,
yearGrade, graduationYear, house,
email, lastName, firstName);
return tempStudent;
}
/**
* Read a Student at some offset
*/
public Student readRecord
(int offset
) throws IOException {
try
{
// Seek to the position
raf.seek(offset * recordSize);
// Read the Username into a byte[]
byte[] usernameTemp = new byte[Student.USERNAME_BYTES];
raf.readFully(usernameTemp);
// Convert the byte[] to a String
// Read the homeroom into a byte[]
byte[] homeroomTemp = new byte[Student.HOMEROOM_BYTES];
raf.readFully(homeroomTemp);
// Convert the byte[] to a String
// Read the id and gender
int id = raf.readInt();
boolean gender = raf.readBoolean();
// Read the birthdate into a byte[]
byte[] birthdateTemp = new byte[Student.BIRTHDATE_BYTES];
raf.readFully(birthdateTemp);
// Convert the byte[] to a String
// Read the birthplace into a byte[]
byte[] birthplaceTemp = new byte[Student.BIRTHPLACE_BYTES];
raf.readFully(birthplaceTemp);
// Convert the byte[] to a String
//Read the locker, year grade and graduation year
int locker = raf.readInt();
int yearGrade = raf.readInt();
int graduationYear = raf.readInt();
// Read the house into a byte[]
byte[] houseTemp = new byte[Student.HOUSE_BYTES];
raf.readFully(houseTemp);
// Convert the byte[] to a String
// Read the email into a byte[]
byte[] emailTemp = new byte[Student.EMAIL_BYTES];
raf.readFully(emailTemp);
// Convert the byte[] to a String
// Read the lastName into a byte[]
byte[] lastNameTemp = new byte[Student.LASTNAME_BYTES];
raf.readFully(lastNameTemp);
// Convert the byte[] to a String
// Read the firstName into a byte[]
byte[] firstNameTemp = new byte[Student.FIRSTNAME_BYTES];
raf.readFully(firstNameTemp);
// Convert the byte[] to a String
// Create a new Student object
Student tempStudent = new Student(username, homeroom, id, gender,
birthdate, birthplace, locker,
yearGrade, graduationYear, house,
email, lastName, firstName);
// Return the video
return (tempStudent);
}
{
throw e;
}
}
/**
* Checks each record for fields. If any are blank, deems record to be empty.
* Saves first filled record to firstStudent, last filled record to
* lastStudent, and number of filled records to numOfStudents.
*/
{
try
{
//Scans whole RAF file
for (int i = 1; i <= numOfRecords; i++)
{
//reads record
tempStudent = readRecord(i);
/*
* saves last record with student as lastStudent, and increments
* numOfStudents for each Student read
*/
if (checkRecord(tempStudent))
{
lastStudent = i;
numOfStudents++;
}
}
//Scans RAF file from the end
for (int i = numOfRecords; i >= 1; i--)
{
//reads record
tempStudent = readRecord(i);
/*
* saves earliest record with student as firstStudent
*/
if (checkRecord(tempStudent))
{
firstStudent = i;
}
}
{
throw e;
}
}