This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes about object serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "about object serialization" Watch "about object serialization" New topic

about object serialization

david hu
Ranch Hand

Joined: Jul 20, 2001
Posts: 143
Can anyone help me look at the following code to help me find the problem? I wish to first write into a file two vectors,and then read it out from this file. But there's always a exception when I read the file.
The exception is: Expecting code
at untitled8.Untitled1.<init>(
at untitled8.Untitled1.main(
--------------my code---------------------------------
import java.util.*;
public class Untitled1 {
public Untitled1() {
FileOutputStream fo = new FileOutputStream("test1.txt");
ObjectOutputStream oos = new ObjectOutputStream(fo);
Vector[] aa = new Vector[2];
aa[0] = new Vector();
aa[1] = new Vector();
aa[0].add(new Integer(120));
aa[1].add(new Integer(111));
for(int i=0; i<aa.length; i++){
} catch(Exception e){e.printStackTrace();}
FileInputStream fi = new FileInputStream("test1.txt");
ObjectInputStream oi = new ObjectInputStream(fi);
Vector tmpv;
tmpv = (Vector)oi.readObject() ;
if (tmpv != null)
else break;
}catch(Exception e){e.printStackTrace();}
public static void main(String[] args){
Untitled1 aa = new Untitled1();
Philippe Maquet

Joined: Jun 02, 2003
Posts: 1872
Hi david,
I didn't make any test, but from reading your code, it seems that you write the Vector contents, while you try to read the Vector itself.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

Any attempt to read object data which exceeds the boundaries of the custom data written by the corresponding writeObject method will cause an OptionalDataException to be thrown with an eof field value of true.

david hu
Ranch Hand

Joined: Jul 20, 2001
Posts: 143
How to make sure that my code doesn't exceed the boundary when I tried to read sequences of objects from a file?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

catch OptionalDataException.
Don't get me started about those stupid light bulbs.
subject: about object serialization
Similar Threads
Can we Serialize a HashMap object in a Thread
How to identify the end of an ObjectInputStream
Problem with serializing a class
print statements not working in serialization code
basic concepts on Serialization