Reading/Writing with ObjectInputStream

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

Reading/Writing with ObjectInputStream

 
0
  #1
May 27th, 2009
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?

  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!
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: Reading/Writing with ObjectInputStream

 
0
  #2
May 27th, 2009
BlueBerry class must implements java.io.Serializable interface.
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: Reading/Writing with ObjectInputStream

 
0
  #3
May 27th, 2009
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?
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