943,724 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1047
  • Java RSS
May 27th, 2009
0

Reading/Writing with ObjectInputStream

Expand Post »
Hello,
I am having problems using an ObjectInputStream to read from a file. My program uses a File to check whether or not a specific file exists. If it exists my program will attempt to create a FileInputStream for the file and associate an ObjectInputStream with it. It should first read an int, then start reading objects. For some reason it returns an I/O error whenever I attempt to run it.

The file itself is created by using a FileOutputStream and associating an ObjectOutputStream with it. I then write an int and my objects follow that. Any ideas what my problem could be?

Java Syntax (Toggle Plain Text)
  1. File checkFile = new File("test.tst");
  2. if(checkFile.exists())
  3. {
  4. ObjectInputStream readFile = new ObjetInputStream(new FileInputStream("test.tst");
  5. int bushels = readFile.readInt();
  6. BlueBerry firstObject = (BlueBerry) readFile.readObject();
  7. BlueBerry secondObject = (BlueBerry) readFile.readObject();
  8. //....
  9. }
The test.tst file is written in such a way that it matches this reading format. If anyone can see an error in how this works I would appreciate it or any sample code I could reference! Thanks!
Similar Threads
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
May 27th, 2009
0

Re: Reading/Writing with ObjectInputStream

BlueBerry class must implements java.io.Serializable interface.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 27th, 2009
0

Re: Reading/Writing with ObjectInputStream

It already does but thanks! I'm assuming that integers can be mixed in the same file as other objects. Is there another way to save that I could try?
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: Jtext area
Next Thread in Java Forum Timeline: Java accessor naming conventions





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


Follow us on Twitter


© 2011 DaniWeb® LLC