Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Error in reading Serialized info Vector objects issues!!

Reply
Thread Tools

Error in reading Serialized info Vector objects issues!!

 
 
rohit
Guest
Posts: n/a
 
      05-01-2005
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

 
Reply With Quote
 
 
 
 
Ross Bamford
Guest
Posts: n/a
 
      05-01-2005
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/ + http://www.velocityreviews.com/forums/(E-Mail Removed)in


 
Reply With Quote
 
 
 
 
rohit
Guest
Posts: n/a
 
      05-02-2005
Thanks a lot!!
It really worked.
Rohit

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading C# serialized objects into Python? Alex van der Spek Python 2 01-08-2009 12:14 PM
Reading delimited gzipped serialized objects Dave Brown Java 7 03-10-2006 03:20 PM
Which objects or classes cannot be serialized? 'not serializable' error Will Java 3 02-19-2006 11:27 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Java Serialized objects Chandrashekar Tippur Java 6 04-25-2004 11:32 PM



Advertisments