[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 Discussion Refresh page to view new replies
 Java Help Needed Techguys
[VIEWED 3249 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 200 days
Recommended Popular Threads Controvertial Threads
What stocks to buy -- Any recommendation
TRUMP 2016!!! Here is why?
महानियिका प्रियंका कार्कीजीलाइ खुल्ला पत्र !
TPS FEDERAL NOTICE
UNDERWEAR
ट्रंप २०२०
Anyone renewed their passport through DC embassy lately?
❤️ सम्मर लभ ❤️
मलाई ब्रेड खायो कि सारै पाद आउछ..
tesla stock OMG !!
I m offered a Job in Bank in Nepal, should i go back ?
Wedding Costs in Nepal ? Party, Band Baaja !!!!???
हाकिमनी संग- आज सुक्रबार -३
Dr. Fauci न टोक्नु न बोक्नु भएको छ ट्रुम्पलाई
ओपन challenge फर गनाउने (underwear)
I met her on Sajha ...
नेपाली समुदायको भारी समर्थन Iowa caucus मा Bernie Sanders लाई - PHOTOS
##केटा हो ,*Monthly* कती *SAVE* हुन्छ ? ##
Why you should vote for Bernie Sanders
नो ट्रम्प २०२०
भात भन्या भात हो!
महानियिका प्रियंका कार्कीजीलाइ खुल्ला पत्र !
TRUMP 2016!!! Here is why?
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