|
Example: information about (many) employees
+-----------+--------+---------+------+-----------+----------+ | ssn | fname | lname | sex | bdate | salary | +-----------+--------+---------+------+-----------+----------+ | 123456789 | John | Smith | M | 09-JAN-55 | 30000.00 | | 333445555 | Frankl | Wong | M | 08-DEC-45 | 40000.00 | | 999887777 | Alicia | Zelaya | F | 19-JUL-58 | 25000.00 | | 987654321 | Jennif | Wallace | F | 20-JUN-31 | 43000.00 | | 666884444 | Ramesh | Narayan | M | 15-SEP-52 | 38000.00 | | 453453453 | Joyce | English | F | 31-JUL-62 | 25000.00 | | 987987987 | Ahmad | Jabbar | M | 29-MAR-59 | 25000.00 | | 888665555 | James | Borg | M | 10-NOV-27 | 55000.00 | +-----------+--------+---------+------+-----------+----------+ |
Each "information unit" (for an employee) has the same structure:
|
|
Example: an employee record
+-----------+--------+---------+------+-----------+----------+ | 123456789 | John | Smith | M | 09-JAN-55 | 30000.00 | +-----------+--------+---------+------+-----------+----------+ |
|
Example: format of the employee record is:
SSN integer fname String (30) lname String (30) sex String (1) bdate String (15) salary float |
Important fact:
|
+-------------+--------------------------------+-------+ | ID | Name | level | +-------------+--------------------------------+-------+ 10 chars 30 chars 4 chars |
How to read 2 records from this data file:
public static void main(String[] args) throws IOException { DataFile myFile = new DataFile("student-file"); // Open student-file String ID, name, level; // Variables to store Strings from file // Read and print the first record ID = myFile.ReadString(10); name = myFile.ReadString(30); level = myFile.ReadString(4); System.out.println(">> " + ID + ", " + name + ", " + level); // Read and print the second record ID = myFile.ReadString(10); name = myFile.ReadString(30); level = myFile.ReadString(4); System.out.println(">> " + ID + ", " + name + ", " + level); } |
Demo:
cd /home/cs377001/demo/Phys-Data-Dependance/file1 java read2StuRec |