Posted by: zeePa May 24, 2009
Java Help Needed Techguys
Login in to Rate this Post:     0       ?        

This is about serialization and deserialization in java.


This week, you'll be writing objects to a file and reading objects from a file. The ReadContactFile code has been done for you, and must be used as is. This file is attached to the assignment link. Your job is to create the files that can use this ReadContactFile code.


Time to put it all together. Make sure the program works, and include the three files; the object file, the write file, and the data file. Do not include the ReadContactFile code, as I'll insert the pre-written one!
ReadContactFile is as follows:


import java.io.*;
public class ReadContactFile{


 public static void main(String [] args) throws IOException, ClassNotFoundException {
  
  ObjectInputStream in = new ObjectInputStream(new FileInputStream("Contacts.dat"));
  Contact con;
  
  try {
   while(true) {
    con = (Contact)in.readObject();
    con.display();
   }
 }
 catch(EOFException e) {
  in.close();
 }
   }
   
}


 


I coded Contact.java as follows:


import java.io.*;


import java.lang.String;


public class Contact implements Serializable


{


private int contact_ID;


private String fname;


private String lname;


private String phone_Number;


public Contact(int contact_ID, String fname, String lname, String phone_Number)


{


this.contact_ID = contact_ID;


this.fname = fname;


this.lname = lname;


this.phone_Number = phone_Number;


}


public int getContact_ID()


{


return contact_ID;


}


public void setContact_ID (int contact_ID)


{


this.contact_ID = contact_ID;


}


public String getFname()


{


return fname;


}


public void setFname(String fname)


{


this.fname = fname;


}


 


public String getLname()


{


return lname;


}


public void setLname(String lname)


{


this.lname = lname;


}


public String getPhone_Number()


{


return phone_Number;


}


public void setPhone_Number(String phone_Number)


{


this.phone_Number = phone_Number;


}


}



I coded the writer file as follows:


import java.io.*;


import java.util.ArrayList;


import java.util.List;


public class PersonDetails {


public static void main(String[] args) {


String filename = "Contacts.dat";


Contact person1 = new Contact(12345, "Rita", "Chaplin", "4023121430");


Contact person2 = new Contact(23456, "Bob", "Adams", "3023452345");


Contact person3 = new Contact(34567, "Richa", "Butler", "4034423421");


Contact person4 = new Contact(45345, "Jimmy", "Edwards", "5453451234");


List list = new ArrayList();


list.add(person1);


list.add(person2);


list.add(person3);


list.add(person4);


FileOutputStream fos = null;


ObjectOutputStream out = null;


try {


fos = new FileOutputStream(filename);


out = new ObjectOutputStream(fos);


out.writeObject(list);


out.close();


} catch (IOException ex) {


ex.printStackTrace();


}


}


}


 


Now the problem is when i try to compile ReadContacFile.java, it gives me an error saying unable to cast.Please help!!

Read Full Discussion Thread for this article