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
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