Error in reading Serialized info Vector objects issues!!

Discussion in 'Java' started by rohit, May 1, 2005.

  1. rohit

    rohit Guest

    Hi all,

    I have two classes :CBase and CHelper class in a package and
    serializing some information in a file and then reading it. I am facing
    some problem while reading the data which was stored using vector of
    objects. Below is the detailed problem:::::


    ////////////CHELPER
    public class CHelper implements Externalizable, Cloneable {

    public string szname;
    //..other functions writeexternal read external

    }

    ////////////CBASE
    public class CBase
    implements Externalizable, Cloneable {
    protected boolean bRead;
    protected boolean bWrite;
    protected Vector vData;

    public void addInstance(CHelper obj) {
    vData.addElement(obj);
    }
    //..other functions writeexternal read external
    }

    **********
    I am Serializing all the CBase information in a file and trying to read
    the contents from this serialized file::::
    ***********
    CBase base = new CBase();
    CHelper obj = new CHelper();
    obj.setName("HELPER");
    base.addInstance(obj); // ADDING THE OBJECT DATA INTO THE VECTOR
    base.bRead = true;
    base.bWrite = true;

    // Writing to the file
    FileOutputStream fs = new FileOutputStream("d:\\temp\\Base.txt");
    ObjectOutputStream os = new ObjectOutputStream(fs);
    os.writeObject(base);
    os.close();

    //reading from the file
    FileInputStream fi = new FileInputStream("d:\\temp\\Base.txt");
    ObjectInputStream oi = new ObjectInputStream(fi);
    CBase read = (CBase) oi.readObject();

    **************
    PRINTING THE OUTPUT
    ***************
    System.out.println("Vector = " +
    read.vData.firstElement().toString());
    System.out.println("Write = " + read.bRead);
    System.out.println("Read = " + read.bWrite);


    The output is something like:

    Vector = com.sp.CHelper@14b7453 // WHAT IS THIS ?? GARBAGE ?? NOT ABLE
    TO ANALYSE :(
    Write = true;
    Read = true;************
    MY PROBLEM:
    i.e How to get the information stored in the Vector ? I am getting
    something "com.sp.CHelper@14b7453 " whereas the expected output should
    be Vector = HELPER. I am writing and reading the vector as follows:
    ***************

    // CHELPER
    public void writeExternal(ObjectOutput out) throws IOException {
    out.writeUTF(szName.toString());
    }
    public void readExternal(ObjectInput in) throws IOException,
    ClassNotFoundException {
    setName(in.readUTF());
    }

    Externalize in CBase Classs
    out.writeInt(getInstanceCount()); // getting number of objects in
    the vector
    for (int index = 0; index < getInstanceCount(); index++) {
    out.writeObject((CHelper)vData.get(index));
    }
    out.writeBoolean(bWrite);
    out.writeBoolean(bRead);

    And reading like this::
    int iNumberofInstances; // number of instances
    iNumberofInstances = in.readInt();
    vData.clear();

    // Get the CHelper object and add it into the CFeatureBase vector
    for (int i = 0; i < iNumberofInstances; i++) {
    CHelper objbase = (CHelper)in.readObject();
    vData.add(objbase);
    }
    this.bWrite = in.readBoolean();
    this.bRead = in.readBoolean();

    I suppose that i am correctly WRITING AND READING the data. B/w what
    can be the problem. All suggestions are welcome/
    Thanks,
    Rohit
    rohit, May 1, 2005
    #1
    1. Advertising

  2. rohit

    Ross Bamford Guest

    On Sun, 2005-05-01 at 05:21 -0700, rohit wrote:
    > Hi all,
    >
    > I have two classes :CBase and CHelper class in a package and
    > serializing some information in a file and then reading it. I am facing
    > some problem while reading the data which was stored using vector of
    > objects. Below is the detailed problem:::::
    >
    > ... [snip] ...
    >
    > The output is something like:
    >
    > Vector = com.sp.CHelper@14b7453 // WHAT IS THIS ?? GARBAGE ?? NOT ABLE
    > TO ANALYSE :(
    > Write = true;
    > Read = true;************
    > MY PROBLEM:
    > i.e How to get the information stored in the Vector ? I am getting
    > something "com.sp.CHelper@14b7453 " whereas the expected output should
    > be Vector = HELPER. I am writing and reading the vector as follows:
    > ***************


    Which suggests that your method is working.

    (From the J2SE API Reference):
    The toString method for class Object returns a string consisting of the
    name of the class of which the object is an instance, the at-sign
    character `@', and the unsigned hexadecimal representation of the hash
    code of the object. In other words, this method returns a string equal
    to the value of:

    getClass().getName() + '@' + Integer.toHexString(hashCode())

    +++ [end quote] +++


    So you print out the first element of your vector (implicitly using toString())
    your classname '@' the hashcode (in hex).

    The easiest way to get what you want is to add this to your 'CHelper':

    public String toString()
    {
    return getName();
    }

    (assuming you have a getter equivalent to your setName method).

    Hope that helps,
    Ross

    --
    [Ross A. Bamford] [ross AT the.website.domain]
    Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
    http://www.roscopec0.f9.co.uk/ + in
    Ross Bamford, May 1, 2005
    #2
    1. Advertising

  3. rohit

    rohit Guest

    Thanks a lot!!
    It really worked.
    Rohit
    rohit, May 2, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chandrashekar Tippur

    Java Serialized objects

    Chandrashekar Tippur, Apr 25, 2004, in forum: Java
    Replies:
    6
    Views:
    445
    Roedy Green
    Apr 26, 2004
  2. Will
    Replies:
    3
    Views:
    10,463
  3. Dave Brown
    Replies:
    7
    Views:
    566
    Roedy Green
    Mar 10, 2006
  4. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  5. Alex van der Spek

    Reading C# serialized objects into Python?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    2
    Views:
    675
    Steve Holden
    Jan 8, 2009
Loading...

Share This Page