Storing classes in files

Reply

Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Storing classes in files

 
0
  #1
May 23rd, 2009
Hello,
I am trying to do something very simple but my dial up connection is making it hard to research so I'd like some help! I'm trying to store instances of classes I created in a file. Something along these lines:

  1. class apple
  2. {
  3. int flavor;
  4. String color;
  5. apple(int a, String b)
  6. {
  7. flavor = a;
  8. color = b;
  9. }
  10. }
  11.  
  12. //.....
  13.  
  14. apple RedDelicious = new apple(10, "red");
  15. writeFile.write(RedDelicious);

I know that there is some way to do this through serialization and I've also heard that something along the lines of FileOutputStream/FileInputStream will allow me to write/read the classes but I would appreciate a little guidance on their use in this context. I'm sorry to bother you guys but my dial up is hindering the research process! Thanks!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Storing classes in files

 
0
  #2
May 24th, 2009
Your class must implements a java.io.Serializable Interface and one more thing is that you have to use java.io.ObjectInputStream and java.io.ObjectOutputStream class to read and write objects respectively.

  1. class apple implements java.io.Serializable
  2. {
  3. int flavor;
  4. String color;
  5. apple(int a, String b)
  6. {
  7. flavor = a;
  8. color = b;
  9. }
  10. }
  11.  
  12. //.....
  13.  
  14. apple RedDelicious = new apple(10, "red");
  15. java.io.ObjectOutputStream os=new java.io.ObjectOutputStream(new java.io.FileOutputStream("data.txt"));
  16.  
  17. os.writeObject(RedDelicious);
  18. os.flush();
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: Storing classes in files

 
0
  #3
May 24th, 2009
Thanks for reminding me. It's been awhile since I tried it and one can only research on dial up can only go so long until the patience runs out ha!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC