import java.lancs.Person; import java.util.Vector; import java.util.Enumeration; import java.io.File; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; class PersistentPeople { // // This function writes a collection of people to a file. The // collection of people is specified by the Vector object, and the // name of the file to which the collection is written to is // specified by . // public static void writePeopleToFile (Vector people, String filename) throws Exception { // Create the file File file = new File (filename); // Create the file stream FileOutputStream fileReader = new FileOutputStream (file); // Create the output stream DataOutputStream outputStream = new DataOutputStream (fileReader); // Get the elements of the vector Enumeration iterator = people.elements (); // Go through the elements while (iterator.hasMoreElements ()) { // Get to a person Person person = (Person) iterator.nextElement (); // Write the Forename (on a line) outputStream.writeBytes (person.getForename () + "\n"); // Write the Surname (on a line) outputStream.writeBytes (person.getSurname () + "\n"); // Write the Age (on a line) String ageString = new Integer (person.getAge ()).toString (); outputStream.writeBytes (ageString + "\n"); // Write the Gender (on a line) String genderString = new Integer (person.getGender ()).toString (); outputStream.writeBytes (genderString + "\n"); } } // // This function reads a collection of people from a file. The // collection of people is return by the Vector object that is // returned, and the name of the file from which the collection is // read is specified by . // public static Vector readPeopleFromFile (String filename) throws Exception { // Create the file File file = new File (filename); // Create the file stream FileInputStream fileReader = new FileInputStream (file); // Create the input stream DataInputStream inputStream = new DataInputStream (fileReader); // Create an (empty) collection Vector people = new Vector (); while (true) { // Read the Forename String forename = inputStream.readLine (); // Make sure we haven't reach the end of the file if (forename == null) { break; } // Read the Surname String surname = inputStream.readLine (); // Read the Age String ageString = inputStream.readLine (); int age = Integer.parseInt (ageString); // Read the Gender String genderString = inputStream.readLine (); int gender = Integer.parseInt (genderString); // Create the person Person person = new Person (forename, surname, age, gender); // Add the new person to the collection people.addElement (person); } // Return the collection return people; } } public class PersistentPeopleTester { public static void main (String[] args) throws Exception { // Create some cool dudes Person irfan = new Person ("Irfan", "Pyarali", 25, Person.MALE); Person frank = new Person ("Frank", "Dokes", 99, Person.MALE); Person karen = new Person ("Karen", "Sanders", 59, Person.FEMALE); // Create a vector Vector people = new Vector (); // Add the cool dudes to the vector people.addElement (irfan); people.addElement (frank); people.addElement (karen); // Print the people System.out.println (people); // Filename where the data will be written to and read from String filename = "cool_dudes.txt"; // Write the cool dudes to the file PersistentPeople.writePeopleToFile (people, filename); // Read the cool dudes from the file people = PersistentPeople.readPeopleFromFile (filename); // Print the people again; note that this vector should contain // exactly the same set of people as before. System.out.println (people); } }