[Show all top banners]

zeePa
Replies to this thread:

More by zeePa
What people are reading
Subscribers
Subscribers
[Total Subscribers 1]

paroo
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Java Help Needed Techguys
[VIEWED 7999 TIMES]
SAVE! for ease of future access.
Posted on 05-24-09 4:12 AM     Reply [Subscribe]
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.


  • Create a class Contact, with an integer contact ID, a first name, a last name, and a phone number. (If you look the ReadContactFile code, you'll see that your class has to have a specific method to output the information)
  • Create the code to write out at least 4 contacts to a file. File name should be the same as the ReadContactFile code file name.

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!!


 
Posted on 05-24-09 11:08 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

It should be:

ArrayList list = new ArrayList();

 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 7 days
Recommended Popular Threads Controvertial Threads
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
I hope all the fake Nepali refugee get deported
All the Qatar ailines from Nepal canceled to USA
MAGA मार्का कुरा पढेर दिमाग नखपाउनुस !
Travel Document for TPS (approved)
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters