aspose file tools*
The moose likes I/O and Streams and the fly likes problem with reading multiple objects from file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem with reading multiple objects from file" Watch "problem with reading multiple objects from file" New topic
Author

problem with reading multiple objects from file

walid aly
Ranch Hand

Joined: Nov 06, 2002
Posts: 38
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

Joined: Jan 04, 2003
Posts: 867
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
SCJP, MCDBA
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Ar....
One thing I want to know is that
do the two objects implements the Serializable marker interface?
thanks
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi walid,
does this post address the problem u have?
regards
maulin
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
One more question
Do the serialized objects contain any static variable or method?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Jan 04, 2003
Posts: 867
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with reading multiple objects from file
 
Similar Threads
De-Serialization is returning Null values
Object Serialization
serialization problem
Writing Integer Object to disk
Plz help me............