Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with reading multiple objects from file

 
walid aly
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All
i am haveing a problem
i write 2 objects to the same file , by running the .class twice towards the same stream , and it goes ok.
when i try to read the 2 objects only one is read and reading the second throws StreamCorruptedException
////////////
FileOutputStream f=new FileOutputStream("s.txt",true);
ObjectOutputStream o=new ObjectOutputStream(f);
o.writeObject(d1);
o.flush();
o.close();

/////////////////////////

FileInputStream f=new FileInputStream("s.txt");
ObjectInputStream i=new ObjectInputStream(f);
d d1=(d)i.readObject();
System.out.println(d1.x);
d d2=(d)i.readObject();
System.out.println(d2.x);
/////////////////

any suggestions???

walid
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi walid
your first code
FileOutputStream f=new FileOutputStream("s.txt",true);
ObjectOutputStream o=new ObjectOutputStream(f);
o.writeObject(d1);
Is missing
?o.writeObject(d2);
o.flush();
o.close();

/////////////////////////
your secord code
try{
FileInputStream f=new FileInputStream("s.txt");
ObjectInputStream i=new ObjectInputStream(f);
d d1=(d)i.readObject();
System.out.println(d1.x);
d d2=(d)i.readObject();
System.out.println(d2.x);
}
catch(IOException ioE)
{
ioE.printStackTrace();
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
finally
{
try{
i.close()
}
catch(IOException io_e)
{
io_e.printStackTrace();
}
}
When you write the code of ObjectI/O
you Muse use try-catch block with the IOException and ClassNotFoundException
Can you try it?
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ar....
One thing I want to know is that
do the two objects implements the Serializable marker interface?
thanks
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi walid,
does this post address the problem u have?
regards
maulin
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more question
Do the serialized objects contain any static variable or method?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not?
serialized objects contains all data members unless they are "transient" and provided they are all "serializable" (if they rn't then u get exception)...
regards
maulin
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Do the object which will be serialized that can contains the static varible and method?
Because I am not sure that (I may be day dreaming)I seen to be listen my teacher who told me static varible and method are not serialized
thank you for your time
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variables are not serialized by the writeObject() method. (See the API for confirmation.) If a class has non-constant static data, you must devise some way of storing and recovering this information when you serialize. There are many different ways you might want or need to do this - it really depends on how your class works, and what does the static data represent. For example, if the static data needs to change every time a new instance is created - e.g. if there's a count of total objects, or even a static HashMap containing references to all instances of a class - you may need to provide a custom readObject() method which performs any required updates on the static data as part of the desirialization process.
As for methods - well, all methods are part of the class definintion. There's nothing to serialize. Any object of the class has access to the instance and static methods of the class, because those methods were already loaded when the class was loaded. The only thing that needs to be serialized is instance data.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic