Error in reading Serialized info Vector objects issues!!

R

rohit

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
 
R

Ross Bamford

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top