Core Java - SerializationJava supports object serialisation, which converts an object into a sequence of bytes including its contents, type, and data types. After writing a serialised object to a file, it can be read and deserialised. This means that the type information and bytes can be used to rebuild the object in memory. The process is JVM agnostic, allowing for object serialisation and deserialisation across multiple platforms. The ObjectInputStream and ObjectOutputStream classes provide methods for serialising and deserialising objects. The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out: public final void writeObject(Object x)throws IOExceptionThe method shown above serialises an object and sends it to the output stream. Similarly, the ObjectInputStream class provides a method for deserialising an object: public final Object readObject()throws IOException, ClassNotFoundExceptionThis method extracts the next Object from the stream and deserialises it. To use the return value, cast it to the proper data type. To explain serialisation in Java, I will utilise the Employee class, which we explored earlier in the book. Assume we have the following Employee class that implements the Serializable interface. import java.io.*; public class Employee implements java.io.Serializable{ public String name; public String address; public transient int SSN; public int number; public void mailCheck(){ System.out.println("Mailing a check to "+ name+" "+ address); } }To successfully serialise a class, two conditions must be met: If you want to know whether or not a Java Standard Class is serializable, look at its documentation. The test is simple. If the class supports java.io.If anything is serializable, it is; otherwise, it is not. Serializing an Object : The ObjectOutputStream class is used to serialize an Object. The following SerializeDemo program instantiates an Employee object and serializes it to a file. When the program is done executing, a file named employee.ser is created. The program does not generate any output, but study the code and try to determine what the program is doing. Note : When serializing an object to a file, the standard convention in Java is to give the file a .serextension. import java.io.*; public class SerializeDemo{ public static void main(String[] args){ Employee e =new Employee(); e.name ="Virat"; e.address ="India"; e.SSN =155; e.number =18; try{ FileOutputStream fileOut =new FileOutputStream("employee.ser"); ObjectOutputStream out=new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); }catch(IOException i){ i.printStackTrace(); } } }Deserializing an Object : Observe the following To deserialise the Employee object created in the SerializeDemo programme, run the DeserializeDemo programme. Examine the programme and try to predict its output: import java.io.*; public class DeserializeDemo{ public static void main(String[] args){ Employee e =null; try{ FileInputStream fileIn =new FileInputStream("employee.ser"); ObjectInputStream in=new ObjectInputStream(fileIn); e =(Employee)in.readObject(); in.close(); fileIn.close(); }catch(IOException i){ i.printStackTrace(); return; }catch(ClassNotFoundException c){ System.out.println("Employee class not found"); c.printStackTrace(); return; } System.out.println("Deserialized Employee..."); System.out.println("Name: "+ e.name); System.out.println("Address: "+ e.address); System.out.println("SSN: "+ e.SSN); System.out.println("Number: "+ e.number); } }Output : Deserialized Employee... Name: Virat Address: India SSN: 0 Number: 18 Here are following important points to be noted: ☛ Join to Learn from Experts: Core Java Training in Chennai by TesDBAcademy
« Previous
Next Topic »
(Core Java - Networking)
|