Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Objectuinputstream

 
viswanath sigamala
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

FileInputStream fis = new FileInputStream("c:\\nonav.log");
ObjectInputStream ois = new ObjectInputStream(fis);
String today = (String) ois.readObject();
byte[] bytedata= today.getBytes();

Above code i am getting below exception:

java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:768)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:283)
at com.ibm.test.ReadZipPObjectinputstream.main(ReadZipPObjectinputstream.java:56)


Please help me in resolving the issue.

Thanks,
Viswanath
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you write nonav.log with an ObjectOutputStream? If not, you cannot read it with an ObjectInputStream.
 
Prakash Subramanian
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also check whether your ObjectOutputStream was flushed and closed properly.
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prakash Subramanian:
Also check whether your ObjectOutputStream was flushed and closed properly.


Prakash, can not flushing and closing of ObjectOutputStream cause this exception? I have doubts.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it caused this exception, but it can sure cause exceptions.

I once closed the backing OutputStream instead of the ZipOutputStream, and as a result the file was a corrupt ZIP file.
 
viswanath sigamala
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys for Help.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it could possibly cause this exception - for example, if the full header never got written in the first place, because some of it was still in a buffer somewhere when the program terminated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic