[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 5748 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
शीर्षक जे पनि हुन सक्छ।
NRN card pros and cons?
What are your first memories of when Nepal Television Began?
TPS Re-registration
Democrats are so sure Trump will win
is Rato Bangala school cheating?
Basnet or Basnyat ??
nrn citizenship
Sajha has turned into MAGATs nest
Nas and The Bokas: Coming to a Night Club near you
डलराँ कमाएर ने .रु मा उडांउदा !@#
ChatSansar.com Naya Nepal Chat
डीभी परेन भने खुसि हुनु होस् ! अमेरिकामाधेरै का श्रीमती अर्कैसँग पोइला गएका छन् !
3 most corrupt politicians in the world
if you are in USA illegally and ask for asylum it is legal
Returning to Nepal with us citizenship. Property ownership
Top 10 Anti-vaxxers Who Got Owned by COVID
आज बाट तिहारको सेल सकियो
निगुरो थाहा छ ??
Do nepalese really need TPS?
Nas and The Bokas: Coming to a Night Club near you
Mr. Dipak Gyawali-ji Talk is Cheap. US sends $ 200 million to Nepal every year.
Harvard Nepali Students Association Blame Israel for hamas terrorist attacks
TPS Update : Jajarkot earthquake
is Rato Bangala school cheating?
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