All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.
Hi,
I am trying to use the customized serialization of the following example but no luck so for. Could you please write the full example? How to use this code in an example? Thanks in advance.
class Bond // does not implement Serializable
{
String ticker = "bac"; double coupon = 8.3; java.util.Date maturity = new Date();
}
class Portfolio implements Serializable
{
String accountName;
transient Bond[] bonds = new Bond[]{ }; // must be transient because Bond class does not implement Serializable
private void writeObject(ObjectOutputStream os) throws Exception{
os.defaultWriteObject();
os.writeInt(bonds.length);
//write the state of bond objects
for(int i=0; i<bonds.length; i++) {
os.writeObject(bonds[i].ticker);
os.writeDouble(bonds[i].coupon);
os.writeObject(bonds[i].maturity);
}
}
private void readObject(ObjectInputStream os) throws Exception{
os.defaultReadObject();
int n = os.readInt();
this.bonds = new Bond[n];
//read the state of bond objects.
for(int i=0; i<bonds.length; i++) {
bonds[i] = new Bond();
bonds[i].ticker = (String) os.readObject();
bonds[i].coupon = os.readDouble();
bonds[i].maturity = (java.util.Date) os.readObject();
}
}
}
It is complete code for the serialization/deserialization part. Where exactly are you facing a problem? Please post the exact code that you are actually trying to execute.
If you like our products and services, please help us by posting your review here.
ya, it is complete code for the serialization/deserialization part.But the problem is, i don't know how to execute it? write the data to file and read from the file. I tried this code but too many errors and don't know how to fix the code. Thanking you in advance.
class Bond // does not implement Serializable
{
String ticker = "bac"; double coupon = 8.3; java.util.Date maturity = new Date();
}
class Portfolio implements Serializable
{
private static final long serialVersionUID = -5778962857540547749L;
String accountName = "xyz"; //added xyz
transient Bond[] bonds = new Bond[]{ }; // must be transient because Bond class does not implement Serializable
//added code ******************************************************
public static void main(String[] args)
{
Portfolio pfolio = new Portfolio();
//write to a file
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myMap.data")))
{
((ObjectOutput) pfolio).writeObject(oos);
}
//read to output
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myMap.data")))
{
((ObjectOutput) pfolio).readObject(ois);
}
//added code************************************************
private void writeObject(ObjectOutputStream os) throws Exception{
os.defaultWriteObject();
os.writeInt(bonds.length);
//write the state of bond objects
for(int i=0; i<bonds.length; i++) {
os.writeObject(bonds[i].ticker);
os.writeDouble(bonds[i].coupon);
os.writeObject(bonds[i].maturity);
}
}
private void readObject(ObjectInputStream os) throws Exception{
os.defaultReadObject();
int n = os.readInt();
this.bonds = new Bond[n];
//read the state of bond objects.
for(int i=0; i<bonds.length; i++) {
bonds[i] = new Bond();
bonds[i].ticker = (String) os.readObject();
bonds[i].coupon = os.readDouble();
bonds[i].maturity = (java.util.Date) os.readObject();
}
}
}
}
Hi
I was just wondering if you guys can help me out.
I tried to implement this example but I doesn't print the Bond class array values.
In the writeObject() method, the bond.length = 0.
Any help will be very much appreciated.
Thanks in advance
Hi I don't have my own code. I have been trying to implement the above code but when i check the bonds array length it's always 0.
Object obj = ois.readObject();
System.out.println(obj);
// how to print values of obj, it just prints "ioStream.Enthware_SerializationExample@60354949"
Well, what is the length of bonds array before saving it to the file? You will get the same length back. You have to change the code to create the bonds array of different length and initialize its elements with Bond object before serialization to get it back after deserialization. In the above code, your bonds array is of length zero, so you are getting 0 back!
To print a meaningful value using println(obj), you need to override the toString method in your class. This is OCAJP stuff
Paul.
If you like our products and services, please help us by posting your review here.