943,883 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 340
  • Java RSS
May 23rd, 2009
0

Storing classes in files

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
May 24th, 2009
0

Re: Storing classes in files

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.

Java Syntax (Toggle Plain Text)
  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();
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 24th, 2009
0

Re: Storing classes in files

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!
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Creating an object and getting a value from it
Next Thread in Java Forum Timeline: Interacting with other windows?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC