write mutiple object states of serilizable object.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akb
    New Member
    • Feb 2007
    • 4

    write mutiple object states of serilizable object.

    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 StreamCorrupted Exception exception thrown.

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

    so plz help me.
    Regards.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by akb
    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 StreamCorrupted Exception exception thrown.

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

    so plz help me.
    Regards.
    Can we see the codes.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by akb
      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 StreamCorrupted Exception exception thrown.

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

      so plz help me.
      Regards.
      Unless you reset the ObjectOutputStr eam 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

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        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.prin tln("DMJ: " + a + "\t" + b);
        }
        }

        //this is my main class

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

        FileInputStream i = new FileInputStream (file_name);
        ObjectInputStre am in = new ObjectInputStre am(i);
        ((s)in.readObje ct()).display() ; //this is OK ... it shows the first state i saved.
        ((s)in.readObje ct()).display() ;
        //the second call is throwing StreamCorrupted Exception.
        }
        }
        plz help.
        kind regards.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          An ObjectOutputStr eam 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 ObjectOutputStr eam 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

          Comment

          • akb
            New Member
            • Feb 2007
            • 4

            #6
            now the full clearity comes up....
            thanx for ur reply...

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

            Plz help.
            Kind regards.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by akb
              now the full clearity comes up....
              thanx for ur reply...

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

              Plz help.
              Kind regards.
              Open the ObjectOutputStr eam 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 ObjectInputStre am and read all the objects.

              That way the header will only be written once.

              kind regards,

              Jos

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                ok we got the solution ....

                lot of thanxxxx .....

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  Originally posted by JosAH
                  Unless you reset the ObjectOutputStr eam 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
                  What did you mean here ? will you please say in details ?

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by dmjpro
                    What did you mean here ? will you please say in details ?
                    Exactly as I wrote: if you do e.g. something like this:

                    Code:
                    BigObject bo= new BigObject();
                    ObjectOutputStream oos= ...;
                    
                    oos.write(bo);
                    bo.changeState();
                    oos.write(bo);
                    bo.changeState();
                    oos.write(bo);
                    oos.close();
                    The content of that oos will be:

                    <header><BigObj ect content><ref><r ef>

                    Where the <header> part is important to an ObjectInputStre am 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

                    Comment

                    • dmjpro
                      Top Contributor
                      • Jan 2007
                      • 2476

                      #11
                      Originally posted by JosAH
                      Exactly as I wrote: if you do e.g. something like this:

                      Code:
                      BigObject bo= new BigObject();
                      ObjectOutputStream oos= ...;
                      
                      oos.write(bo);
                      bo.changeState();
                      oos.write(bo);
                      bo.changeState();
                      oos.write(bo);
                      oos.close();
                      The content of that oos will be:

                      <header><BigObj ect content><ref><r ef>

                      Where the <header> part is important to an ObjectInputStre am 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

                      Wow it saves the memory... cool ;)
                      One thing tell me what's the role of clone in serialization?

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by dmjpro
                        Wow it saves the memory... cool ;)
                        One thing tell me what's the role of clone in serialization?
                        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

                        Comment

                        Working...