customize the serialization. enthuware.ocpjp.v7.2.1389

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.

Moderators: Site Manager, fjwalraven

Post Reply
itsriaz
Posts: 10
Joined: Tue Nov 18, 2014 10:01 am
Contact:

customize the serialization. enthuware.ocpjp.v7.2.1389

Post by itsriaz »

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.

Code: Select all

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();
	}
	
    }    
}

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by admin »

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.

itsriaz
Posts: 10
Joined: Tue Nov 18, 2014 10:01 am
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by itsriaz »

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.

Code: Select all

 
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();
	}
	
    }
    
   } 
}


admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by admin »

This is how your main method should be:

Code: Select all

    public static void main(String[] args) throws Exception{
        Portfolio pf = new Portfolio();
        FileOutputStream fos = new FileOutputStream("c:\\temp\\test.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(pf);
        oos.flush(); oos.close();fos.close();
        
        FileInputStream fis = new FileInputStream("c:\\temp\\test.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object obj = ois.readObject();
        System.out.println(obj);
        
        
    }
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

itsriaz
Posts: 10
Joined: Tue Nov 18, 2014 10:01 am
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by itsriaz »

Excellent.. Thanks very much...

XpressJava
Posts: 3
Joined: Tue Apr 07, 2015 6:53 am
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by XpressJava »

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

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by admin »

Please post exact code that you are trying to run.
If you like our products and services, please help us by posting your review here.

XpressJava
Posts: 3
Joined: Tue Apr 07, 2015 6:53 am
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by XpressJava »

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"


Code: Select all

package ioStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;

class Bond {
	String ticker = "ticker"; double coupon = 8.3;  java.util.Date maturity = new Date();
}

class Enthware_SerializationExample implements Serializable {

	private static final long serialVersionUID = 1L;
	String client = "Client XYZ";
	transient Bond[] bonds= new Bond[]{};
	
	
	public static void main(String[] args) throws ClassNotFoundException, IOException 
	{
		Enthware_SerializationExample  pf = new Enthware_SerializationExample();
	
		
		FileOutputStream fos = new FileOutputStream("test.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(pf);
        oos.flush(); oos.close();fos.close();
        
        FileInputStream fis = new FileInputStream("test.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object obj = ois.readObject();
        System.out.println(obj);
        
	}
	
	private void writeObject(ObjectOutputStream os) throws Exception{
		 
		System.out.println("writing object " + bonds.length);  // why bonds.length=0 ?
		 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();
		   
		   System.out.println("Reading object");
		   
		   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();
		      
		   }
		
		    }

}

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by admin »

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.

XpressJava
Posts: 3
Joined: Tue Apr 07, 2015 6:53 am
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by XpressJava »

Ahaaa....Okay-doke.... I got it.... Thanks very much...

pfilaretov
Posts: 35
Joined: Mon Jul 28, 2014 2:05 am
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by pfilaretov »

Q: What can be done so that a Portfolio object can be serialized while preserving the state of the Bond objects contained in Portfolio? 
A:
  • Just have Bond class implement Serializable.
  • Make bonds array transient in Portfolio and implement readObject and writeObject methods to read and write the state of Bond objects explicitly.
Question doesn't say "independently", so it's confusing why I need to do something else if I already make "Bond class implement Serializable"..

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: customize the serialization. enthuware.ocpjp.v7.2.1389

Post by admin »

Fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest