Serialization of Objects:
We write some programs where we stored only text into the files and retrieved same text from the files.These text files are useful when we do not want to perform any calculations on the data.What happens if we want to store some structured data in the files.
For Example:
class Employee implements Serializable
{
private int id;
private String name;
private float salary;
private Date date;
}
Then create an object to this class and store actual data into that object.Later on this object should be stored into a file using ObjectOutputStream.So,seralizable interface should be implemented by the class whose objects are to be stored into the file.
Serializable interface is an empty interface without any members in it.It does not contain any methods also.Such an interface is called "marking Interface or Tagging Interface".marking interface is useful to mark the objects of a class for a specified purpose.
For example, Serializable interface marks the class,for a specified purpose. So that can be written into the file.If serializable interface is not implemented by the class then writing that class objects into a file will lead to NotSerializableException .
Note: However, any static and transient variables of the class can not be serialized.
What is De-serialization?
Ans: De-Serialization is a process of reading back the objects from a file.
Program:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
/**
*
*/
/**
* @author Abhinaw.Tripathi
*
*/
class Employee implements Serializable
{
private int id;
private String name;
private float sal;
private Date obj;
public Employee(int i,String n,float s,Date d)
{
this.id=i;
this.name=n;
this.obj=d;
this.sal=s;
}
public void display()
{
System.out.println(id + "\n");
System.out.println(name + "\n");
System.out.println(sal + "\n");
System.out.println(obj + "\n");
}
public static Employee getData() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter emp id:" );
int id=Integer.parseInt(br.readLine());
System.out.println("Enter emp name:" );
String name=br.readLine();
System.out.println("Enter emp Salary:" );
float sal=Float.parseFloat(br.readLine());
Date d=new Date();
Employee e=new Employee(id, name, sal, d);
return e;
}
}
public class SerializableStoreObjectApp
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
FileOutputStream fos=new FileOutputStream("objFile");
ObjectOutputStream ois=new ObjectOutputStream(fos);
System.out.println("How many objects?.");
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
Employee e1=Employee.getData();
ois.writeObject(e1);
}
ois.close();
}
}
Output:
How many objects?.
2
Enter emp id:
1
Enter emp name:
abhinaw
Enter emp Salary:
1000
Enter emp id:
2
Enter emp name:
sandeep
Enter emp Salary:
5000
We write some programs where we stored only text into the files and retrieved same text from the files.These text files are useful when we do not want to perform any calculations on the data.What happens if we want to store some structured data in the files.
For Example:
class Employee implements Serializable
{
private int id;
private String name;
private float salary;
private Date date;
}
Then create an object to this class and store actual data into that object.Later on this object should be stored into a file using ObjectOutputStream.So,seralizable interface should be implemented by the class whose objects are to be stored into the file.
Serializable interface is an empty interface without any members in it.It does not contain any methods also.Such an interface is called "marking Interface or Tagging Interface".marking interface is useful to mark the objects of a class for a specified purpose.
For example, Serializable interface marks the class,for a specified purpose. So that can be written into the file.If serializable interface is not implemented by the class then writing that class objects into a file will lead to NotSerializableException .
Note: However, any static and transient variables of the class can not be serialized.
What is De-serialization?
Ans: De-Serialization is a process of reading back the objects from a file.
Program:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
/**
*
*/
/**
* @author Abhinaw.Tripathi
*
*/
class Employee implements Serializable
{
private int id;
private String name;
private float sal;
private Date obj;
public Employee(int i,String n,float s,Date d)
{
this.id=i;
this.name=n;
this.obj=d;
this.sal=s;
}
public void display()
{
System.out.println(id + "\n");
System.out.println(name + "\n");
System.out.println(sal + "\n");
System.out.println(obj + "\n");
}
public static Employee getData() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter emp id:" );
int id=Integer.parseInt(br.readLine());
System.out.println("Enter emp name:" );
String name=br.readLine();
System.out.println("Enter emp Salary:" );
float sal=Float.parseFloat(br.readLine());
Date d=new Date();
Employee e=new Employee(id, name, sal, d);
return e;
}
}
public class SerializableStoreObjectApp
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
FileOutputStream fos=new FileOutputStream("objFile");
ObjectOutputStream ois=new ObjectOutputStream(fos);
System.out.println("How many objects?.");
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
Employee e1=Employee.getData();
ois.writeObject(e1);
}
ois.close();
}
}
How many objects?.
2
Enter emp id:
1
Enter emp name:
abhinaw
Enter emp Salary:
1000
Enter emp id:
2
Enter emp name:
sandeep
Enter emp Salary:
5000
Writing program to de-serialize this object:
Program:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
/**
*
*/
/**
* @author Abhinaw.Tripathi
*
*/
public class GetObjectDeserialize
{
public static void main(String[] args)
{
ObjectInputStream ois = null;
try
{
FileInputStream fis=new FileInputStream("objFile");
ois=new ObjectInputStream(fis);
Employee e;
while((e=(Employee)ois.readObject())!=null)
{
e.display();
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Output:
1
abhinaw
1000.0
Thu Jun 16 17:33:06 IST 2016
2
sandeep
5000.0
Thu Jun 16 17:33:18 IST 2016
No comments:
Post a Comment