Download Object Serialization in Java Serialization in java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Object Serialization in Java
• Serialization in java is a mechanism of writing
the state of an object into a byte stream.
• It is mainly used in Hibernate, RMI, JPA, EJB
and JMS technologies.
• The reverse operation of serialization is called
deserialization.
• Advantage of Java Serialization
• It is mainly used to travel object's state on the
network (known as marshaling).
java.io.Serializable interface
import java.io.Serializable;
public class Student implements Serializable
{
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}
• In the above example, Student class implements Serializable
interface. Now its objects can be converted into stream.
ObjectOutputStream class
• The ObjectOutputStream class is used to write
primitive data types and Java objects to an
OutputStream. Only objects that support the
java.io.Serializable interface can be written to
streams.
import java.io.*;
class Depersist{
public static void main(String args[])throws Exception{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"))
;
Student s=(Student)in.readObject();
System.out.println(s.id+" "+s.name);
in.close();
}
}
OP : 211 ravi
Deserialization in java
• Deserialization is the process of reconstructing the object
from the serialized state.It is the reverse operation of
serialization.
ObjectInputStream class
• An ObjectInputStream deserializes objects and primitive data
written using an ObjectOutputStream.
Example of Java Deserialization
import java.io.*;
class Depersist{
public static void main(String args[])throws Exception{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"))
;
Student s=(Student)in.readObject();
System.out.println(s.id+" "+s.name);
in.close();
}
}
211 ravi
(Internet Inter-ORB Protocol)
• IIOP (Internet Inter-ORB Protocol) is a protocol
that makes it possible for distributed
programs written in different programming
languages to communicate over the Internet.
• The distributed Hello World example uses a
client application to make a remote method
call via IIOP to a server running on the host
from which the client was downloaded. When
the client runs, "Hello from MARS!" is
displayed.
• The steps to write the source files
• The steps to compile the example
• The steps to run the example
Write or Download the Source Files
There are three tasks to complete in this section:
• Define the functions of the remote class as an interface
written in the Java programming language
• Write the implementation class
• Write the server class
• Write a client program that uses the remote service
• The source files used in this tutorial are:
HelloInterface.java - a remote interface
• HelloImpl.java - a remote object implementation
that implements HelloInterface
• HelloServer.java - an RMI server that creates an
instance of the remote object implementation
and binds that instance to a name in the Naming
Service
• HelloClient.java - a client application that invokes
the remote method, sayHello()