473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

write mutiple object states of serilizable object.

4
can i write multiple object states of a serializable in a single file?
if i can then how can i read back those states?

i have no problem with writing multiple object states in a file.
but i can't read back the all object states what i saved so far.

only after reading back the first state then StreamCorruptedException exception thrown.

i know it is due to data inconsistency in the file.

so plz help me.
Regards.
May 9 '07 #1
11 2486
r035198x
13,262 8TB
can i write multiple object states of a serializable in a single file?
if i can then how can i read back those states?

i have no problem with writing multiple object states in a file.
but i can't read back the all object states what i saved so far.

only after reading back the first state then StreamCorruptedException exception thrown.

i know it is due to data inconsistency in the file.

so plz help me.
Regards.
Can we see the codes.
May 9 '07 #2
JosAH
11,448 Expert 8TB
can i write multiple object states of a serializable in a single file?
if i can then how can i read back those states?

i have no problem with writing multiple object states in a file.
but i can't read back the all object states what i saved so far.

only after reading back the first state then StreamCorruptedException exception thrown.

i know it is due to data inconsistency in the file.

so plz help me.
Regards.
Unless you reset the ObjectOutputStream or writeUnshared() every object is
written to such a stream only once. Every following attempt to write the object
only a reference to a previously serialized incarnation is written to the stream.

kind regards,

Jos
May 9 '07 #3
dmjpro
2,476 2GB
actually this Q. is sent by me on behalf of AKB. so i send u the source code.

as per as ro35198x requirements concern ...

my code is something like this .....

//this is my SERIALIZABLE class which is to be serialized.

class s implements Serailizable
{
private int a ,b;
public s(int A,int B){a=A;b=B;}
public void display(){
System.out.println("DMJ: " + a + "\t" + b);
}
}

//this is my main class

class m
{
public static void main(String args[])
{
FileOutputStream o = new FileOutputStream(file_name,true);
//the file opened in append mode
ObjectOutputStream out = new ObjectOutputStream(o);
out.writeObject(new s(100,200));
out.writeObject(new s(300,400));

FileInputStream i = new FileInputStream(file_name);
ObjectInputStream in = new ObjectInputStream(i);
((s)in.readObject()).display(); //this is OK ... it shows the first state i saved.
((s)in.readObject()).display();
//the second call is throwing StreamCorruptedException.
}
}
plz help.
kind regards.
May 9 '07 #4
JosAH
11,448 Expert 8TB
An ObjectOutputStream writes header information to the wrapped stream when
you open/create it. Your file contains the following information:

HHHOOOOO ... OOO

where HHH represents the header information and OOO ... OOO represents
the data for the serialized object itself. When you reopen your file in append
mode again and open an ObjectOutputStream again, the header will also be
written again. After writing another object the file contents look like this:

HHHOOOOO ... OOOHHHOOOOO ... OOO

When you want to read objects from this file you can read the first one but
unexpectedly another header is read when you want to read the second object.

kind regards,

Jos
May 9 '07 #5
akb
4
now the full clearity comes up....
thanx for ur reply...

then what is the solution ... to accomplish this.

Plz help.
Kind regards.
May 9 '07 #6
JosAH
11,448 Expert 8TB
now the full clearity comes up....
thanx for ur reply...

then what is the solution ... to accomplish this.

Plz help.
Kind regards.
Open the ObjectOutputStream once and write all your objects. At the end
close the stream. You can read all your objects later by opening the file,
wrap it in an ObjectInputStream and read all the objects.

That way the header will only be written once.

kind regards,

Jos
May 9 '07 #7
dmjpro
2,476 2GB
ok we got the solution ....

lot of thanxxxx .....
May 9 '07 #8
dmjpro
2,476 2GB
@JosAH
What did you mean here ? will you please say in details ?
Mar 27 '09 #9
JosAH
11,448 Expert 8TB
@dmjpro
Exactly as I wrote: if you do e.g. something like this:

Expand|Select|Wrap|Line Numbers
  1. BigObject bo= new BigObject();
  2. ObjectOutputStream oos= ...;
  3.  
  4. oos.write(bo);
  5. bo.changeState();
  6. oos.write(bo);
  7. bo.changeState();
  8. oos.write(bo);
  9. oos.close();
  10.  
The content of that oos will be:

<header><BigObject content><ref><ref>

Where the <header> part is important to an ObjectInputStream only, <BigObject content> is the content of your object before the two changeState() calls and the <ref> parts are references to that first BigObject content.

kind regards,

Jos
Mar 27 '09 #10
dmjpro
2,476 2GB
@JosAH

Wow it saves the memory... cool ;)
One thing tell me what's the role of clone in serialization?
Mar 27 '09 #11
JosAH
11,448 Expert 8TB
@dmjpro
None; the clone() method isn't used by the serialization framework. It has to save memory as you call it because of circular references: if an object A (which must be Serializable) refers to an object B ( which also has to be Serializable) is serialized both objects A and B are serialized. Now assume object B also refers to object A; you don't want an infinite sequence of serialized objects A, B, A, B, A, B etc. You want a sequence like: <objectA content><object B content><ref to first object A>.

kind regards,

Jos
Mar 27 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: vbvjain | last post by:
Hi Thanks for your suggestion, i even tried that but now the error that is generated is "There was an error generating XML". If i declare the input parameter of my web service method as object and...
0
by: deepak | last post by:
i have set multiple selection property in bith listboxes(html control) to true. i have taken 2 buttons(html control) say Button1,Button2.now i want to add mutiple selected items to another listbox...
0
by: saravana | last post by:
I have to display the mutiple xml filename path in treeview control using c#. net some thing like this In treeview control....... ========= =C:\\a.xml=
8
RobH
by: RobH | last post by:
Hi.. I have a table of Products and StateID. The products with a field for which states they are availble using the codes from the StateID table tbl-Products ProductID Description ...
3
poucedeleon
by: poucedeleon | last post by:
I am building a database for logging in Officers into our building. It uses a continuous form. I have added a command button labeled "Multiple Bookings. OnClick it opens a form that has two...
6
by: shanmugamit | last post by:
hi, var len=document.form.check.length flag=false; for(i=0;i<len;i++) { if(document.form.check.checked==true) { flag=true; break;
7
by: shanmugamit | last post by:
hi, i using mutiple check box but i didn't get all value... as $res=mysql_query("select * from cie where des='Resource Pipeline Associate' order by name"); $j=0;...
5
by: RyanN | last post by:
Hello, I'm trying to teach myself OOP to do a data project involving hierarchical data structures. I've come up with an analogy for testing involving objects for continents, countries, and...
6
by: dewraj | last post by:
Hi I am using windows applicaiton(.net) as front end, and I want to get mutiple tables in Dataset by executing a single stored procedure i.e. SP would return mutiple tables (record sets) with the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.