The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

You must first read data written before object

Posted by Ivan Porty on February 24, 2001 at 10:47 AM

> hi,
> I have opened a file in append mode
> using ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("filename",true));

> now my problem is that when i try to read the file using
> ObjectInputStream in =new ObjectInputStream(new
> FileInputStream("filename"));

> i get an error that
> java.io.StreamCorruptedException: Type code out of range, is -84
> what should i do?

>
> where am i going wrong.

Hi,

you said you opened a file in append mode, so I assume there's
some data in that file you simply didn't want to lose. But when
you open FileInputStream, it positions file cursor at zero and when you try to read back your object, you actually read back some data but not object.
Try this : 1) if you know length of the data in the file, skip that data by using "skip(long toSkip)" and read your object after that
2) always try to write your objects in separate streams, or write data in files using same output stream.
Good Luck, Ivan.




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us