Serialization java example 2013

View previous topic View next topic Go down

Serialization java example 2013

Post by Admin on Sun Sep 15, 2013 4:03 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on">To implement a very basic example of Serialization in java, lets create Person.java that implements Serializable interface.</br><b>Person.java</b></br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.Serializable;<br /> public class Person implements Serializable { <br /> private String firstName; <br /> private String lastName; <br /> public Person(String firstName, String lastName) { <br /> super(); <br /> this.firstName = firstName; <br /> this.lastName = lastName; <br /> } <br /> public String getFirstName() { <br /> return firstName; <br /> } <br /> public void setFirstName(String firstName) { <br /> this.firstName = firstName; <br /> } <br /> public String getLastName() { <br /> return lastName; <br /> } <br /> public void setLastName(String lastName) { <br /> this.lastName = lastName; <br /> } <br /> } <br /></code></pre><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657713065266565&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657725999716598&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657739378994418&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657745934599371&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe></br><b>SerializationMain.java</b></br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.FileOutputStream; <br /> import java.io.IOException; <br /> import java.io.ObjectOutputStream; <br /> public class SerializationMain { <br /> public static void main(String[] args) throws IOException { <br /> Person person = new Person("Kumar", "Bhatia"); <br /> System.out.println("person " + person); <br /> FileOutputStream fos = new FileOutputStream("serial.txt"); <br /> ObjectOutputStream oos = new ObjectOutputStream(fos); <br /> oos.writeObject(person); <br /> oos.flush(); <br /> oos.close(); <br /> } <br /> } <br /></code></pre> If "implements Serializable" is removed from Person.java, following exception occurs:</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">Exception in thread "main" java.io.NotSerializableException: com.serialization.Person<br /> at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)<br /> at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)<br /> at com.serialization.SerializationMain.main(SerializationMain.java:15)<br /></code></pre> <b>DeSerializationMain.java</b></br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.FileInputStream; <br /> import java.io.IOException; <br /> import java.io.ObjectInputStream; <br /> public class DeSerializationMain { <br /> public static void main(String[] args) throws IOException, ClassNotFoundException { <br /> FileInputStream fis = new FileInputStream("serial.txt"); <br /> ObjectInputStream ois = new ObjectInputStream(fis); <br /> Person person = (Person) ois.readObject(); <br /> ois.close(); <br /> System.out.println("person " + person.getFirstName()); <br /> } <br /> } <br /></code></pre> Run DeSerializationMain.java and the output would be same person object that gets serialized.</br><a href="http://1.bp.blogspot.com/-cYexYiyhRgo/Ue-mQDMrC4I/AAAAAAAAAFg/U1LsVIHxb_o/s1600/FileStructure_Serialization.JPG" imageanchor="1" ><img border="0" src="http://1.bp.blogspot.com/-cYexYiyhRgo/Ue-mQDMrC4I/AAAAAAAAAFg/U1LsVIHxb_o/s320/FileStructure_Serialization.JPG" /></a></br></br></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on">To implement a very basic example of Serialization in java, lets create Person.java that implements Serializable interface.</br><b>Person.java</b></br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.Serializable;<br /> public class Person implements Serializable { <br /> private String firstName; <br /> private String lastName; <br /> public Person(String firstName, String lastName) { <br /> super(); <br /> this.firstName = firstName; <br /> this.lastName = lastName; <br /> } <br /> public String getFirstName() { <br /> return firstName; <br /> } <br /> public void setFirstName(String firstName) { <br /> this.firstName = firstName; <br /> } <br /> public String getLastName() { <br /> return lastName; <br /> } <br /> public void setLastName(String lastName) { <br /> this.lastName = lastName; <br /> } <br /> } <br /></code></pre><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657713065266565&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657725999716598&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657739378994418&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe><iframe src="http://www.flipkart.com/affiliate/displayWidget?affrid=WRID-137657745934599371&affid=kumarbhati" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" ></iframe></br><b>SerializationMain.java</b></br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.FileOutputStream; <br /> import java.io.IOException; <br /> import java.io.ObjectOutputStream; <br /> public class SerializationMain { <br /> public static void main(String[] args) throws IOException { <br /> Person person = new Person("Kumar", "Bhatia"); <br /> System.out.println("person " + person); <br /> FileOutputStream fos = new FileOutputStream("serial.txt"); <br /> ObjectOutputStream oos = new ObjectOutputStream(fos); <br /> oos.writeObject(person); <br /> oos.flush(); <br /> oos.close(); <br /> } <br /> } <br /></code></pre> If "implements Serializable" is removed from Person.java, following exception occurs:</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">Exception in thread "main" java.io.NotSerializableException: com.serialization.Person<br /> at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)<br /> at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)<br /> at com.serialization.SerializationMain.main(SerializationMain.java:15)<br /></code></pre> <b>DeSerializationMain.java</b></br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> package com.serialization; <br /> import java.io.FileInputStream; <br /> import java.io.IOException; <br /> import java.io.ObjectInputStream; <br /> public class DeSerializationMain { <br /> public static void main(String[] args) throws IOException, ClassNotFoundException { <br /> FileInputStream fis = new FileInputStream("serial.txt"); <br /> ObjectInputStream ois = new ObjectInputStream(fis); <br /> Person person = (Person) ois.readObject(); <br /> ois.close(); <br /> System.out.println("person " + person.getFirstName()); <br /> } <br /> } <br /></code></pre> Run DeSerializationMain.java and the output would be same person object that gets serialized.</br><a href="http://1.bp.blogspot.com/-cYexYiyhRgo/Ue-mQDMrC4I/AAAAAAAAAFg/U1LsVIHxb_o/s1600/FileStructure_Serialization.JPG" imageanchor="1" ><img border="0" src="http://1.bp.blogspot.com/-cYexYiyhRgo/Ue-mQDMrC4I/AAAAAAAAAFg/U1LsVIHxb_o/s320/FileStructure_Serialization.JPG" /></a></br></br></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum