1. /**
  2.   * Sets length of RAF file to fileLength, and creates "null" students in
  3.   * all records
  4.   */
  5. public void initialize() throws IOException
  6. {
  7. try
  8. {
  9. raf.setLength(fileLength);
  10.  
  11. tempStudent = nullStudent();
  12.  
  13. for (int i = 1; i < numOfRecords; i++)
  14. {
  15. writeRecord(tempStudent, i);
  16. }
  17. }
  18. catch (IOException e)
  19. {
  20. throw e;
  21. }
  22. }
  23.  
  24. /**
  25.   * Returns "null" student
  26.   */
  27. public Student nullStudent() throws IOException
  28. {
  29. String username = "null";
  30. String homeroom = "null";
  31. int id = 0;
  32. boolean gender = false;
  33. String birthdate = "null";
  34. String birthplace = "null";
  35. int locker = 0;
  36. int yearGrade = 0;
  37. int graduationYear = 0;
  38. String house = "null";
  39. String email = "null";
  40. String lastName = "null";
  41. String firstName = "null";
  42.  
  43. //creates "null" student
  44. tempStudent = new Student(username, homeroom, id, gender,
  45. birthdate, birthplace, locker,
  46. yearGrade, graduationYear, house,
  47. email, lastName, firstName);
  48.  
  49. return tempStudent;
  50. }
  51.  
  52. /**
  53.   * Read a Student at some offset
  54.   */
  55. public Student readRecord(int offset) throws IOException
  56. {
  57. try
  58. {
  59. // Seek to the position
  60. raf.seek(offset * recordSize);
  61.  
  62. // Read the Username into a byte[]
  63. byte[] usernameTemp = new byte[Student.USERNAME_BYTES];
  64. raf.readFully(usernameTemp);
  65.  
  66. // Convert the byte[] to a String
  67. String username = new String(usernameTemp).trim();
  68.  
  69. // Read the homeroom into a byte[]
  70. byte[] homeroomTemp = new byte[Student.HOMEROOM_BYTES];
  71. raf.readFully(homeroomTemp);
  72.  
  73. // Convert the byte[] to a String
  74. String homeroom = new String(homeroomTemp).trim();
  75.  
  76. // Read the id and gender
  77. int id = raf.readInt();
  78. boolean gender = raf.readBoolean();
  79.  
  80. // Read the birthdate into a byte[]
  81. byte[] birthdateTemp = new byte[Student.BIRTHDATE_BYTES];
  82. raf.readFully(birthdateTemp);
  83.  
  84. // Convert the byte[] to a String
  85. String birthdate = new String(birthdateTemp).trim();
  86.  
  87. // Read the birthplace into a byte[]
  88. byte[] birthplaceTemp = new byte[Student.BIRTHPLACE_BYTES];
  89. raf.readFully(birthplaceTemp);
  90.  
  91. // Convert the byte[] to a String
  92. String birthplace = new String(birthplaceTemp).trim();
  93.  
  94. //Read the locker, year grade and graduation year
  95. int locker = raf.readInt();
  96. int yearGrade = raf.readInt();
  97. int graduationYear = raf.readInt();
  98.  
  99. // Read the house into a byte[]
  100. byte[] houseTemp = new byte[Student.HOUSE_BYTES];
  101. raf.readFully(houseTemp);
  102.  
  103. // Convert the byte[] to a String
  104. String house = new String(houseTemp).trim();
  105.  
  106. // Read the email into a byte[]
  107. byte[] emailTemp = new byte[Student.EMAIL_BYTES];
  108. raf.readFully(emailTemp);
  109.  
  110. // Convert the byte[] to a String
  111. String email = new String(emailTemp).trim();
  112.  
  113. // Read the lastName into a byte[]
  114. byte[] lastNameTemp = new byte[Student.LASTNAME_BYTES];
  115. raf.readFully(lastNameTemp);
  116.  
  117. // Convert the byte[] to a String
  118. String lastName = new String(lastNameTemp).trim();
  119.  
  120. // Read the firstName into a byte[]
  121. byte[] firstNameTemp = new byte[Student.FIRSTNAME_BYTES];
  122. raf.readFully(firstNameTemp);
  123.  
  124. // Convert the byte[] to a String
  125. String firstName = new String(firstNameTemp).trim();
  126.  
  127. // Create a new Student object
  128. Student tempStudent = new Student(username, homeroom, id, gender,
  129. birthdate, birthplace, locker,
  130. yearGrade, graduationYear, house,
  131. email, lastName, firstName);
  132.  
  133. // Return the video
  134. return (tempStudent);
  135. }
  136. catch (IOException e)
  137. {
  138. throw e;
  139. }
  140. }
  141.  
  142.  
  143. /**
  144.   * Checks each record for fields. If any are blank, deems record to be empty.
  145.   * Saves first filled record to firstStudent, last filled record to
  146.   * lastStudent, and number of filled records to numOfStudents.
  147.   */
  148. public void parseRAF() throws IOException
  149. {
  150. try
  151. {
  152. //Scans whole RAF file
  153. for (int i = 1; i <= numOfRecords; i++)
  154. {
  155. //reads record
  156. tempStudent = readRecord(i);
  157.  
  158. /*
  159.   * saves last record with student as lastStudent, and increments
  160.   * numOfStudents for each Student read
  161.   */
  162. if (checkRecord(tempStudent))
  163. {
  164. lastStudent = i;
  165. numOfStudents++;
  166. }
  167. }
  168.  
  169. //Scans RAF file from the end
  170. for (int i = numOfRecords; i >= 1; i--)
  171. {
  172. //reads record
  173. tempStudent = readRecord(i);
  174.  
  175. /*
  176.   * saves earliest record with student as firstStudent
  177.   */
  178. if (checkRecord(tempStudent))
  179. {
  180. firstStudent = i;
  181. }
  182. }
  183. } catch (IOException e)
  184. {
  185. throw e;
  186. }
  187. }
  188.