Reading/Writing Objects in Java

Serialization is a process in which an object is converted into a sequence of bytes. This conversion allows the object to be easily stored in a persistent manner or transmitted across different platforms or networks. The resulting byte stream can later be deserialized, meaning it can be converted back into an identical replica of the original object. Since the byte stream is platform neutral, objects created in one system can be successfully deserialized in a different platform.

Java object serialization

Serialization is particularly useful when there is a need to transmit object data over a network, such as from one Java Virtual Machine (JVM) to another. In the Java programming language, the serialization mechanism is integrated into the platform. However, to make an object serializable, you must implement the Serializable interface.serializable.

Example
import java.io.*; class Student implements java.io.Serializable{ int ID; String Name; Student(int ID, String Name){ this.ID = ID; this.Name=Name; } }

By implementing the Serializable interface, you enable the serialization process for an object. This interface serves as a marker, indicating that an object's state can be converted into a byte stream. Once an object is marked as serializable, it can be easily saved to a file or sent over a network using methods provided by the Java platform. The serialization and deserialization processes are fundamental for efficient data transfer and storage, allowing developers to transmit complex objects without worrying about the underlying byte-level representation.

How to read and write Java object from/to a file?

The ObjectOutputStream class plays a crucial role in the serialization process by enabling the conversion of objects into a serialized form and writing them to a file. When using this class, developers can efficiently serialize objects and store them persistently for future use. By utilizing the ObjectOutputStream, the serialized objects can be written in a sequential manner, ensuring their integrity and enabling easy retrieval.

On the other hand, the ObjectInputStream class serves as the counterpart to the ObjectOutputStream. It facilitates the deserialization process by reading the serialized objects from a file and reconstructing them back into their original form. Similar to how the ObjectOutputStream writes objects sequentially, the ObjectInputStream reads them in the same order, ensuring the correct reconstruction of the objects.

Example
import java.util.*; import java.io.*; class Student implements java.io.Serializable { int ID; String Name; Student(int ID, String Name){ this.ID = ID; this.Name=Name; } public String toString() { return "ID:" + ID + "\nName: " + Name; } } public class TestClass{ public static void main(String[] args) { Student st1 = new Student(100, "Bill"); Student st2 = new Student(101, "Gates"); //writing Object to file try { FileOutputStream fos = new FileOutputStream(new File("D:\\javaObjects.txt")); ObjectOutputStream oos = new ObjectOutputStream(fos); // Write objects to file oos.writeObject(st1); oos.writeObject(st2); oos.close(); fos.close(); } catch (IOException e){ e.printStackTrace(); } //Read objects from file try{ FileInputStream fis = new FileInputStream(new File("D:\\javaObjects.txt")); ObjectInputStream ois = new ObjectInputStream(fis); Student s1 = (Student) ois.readObject(); Student s2 = (Student) ois.readObject(); System.out.println(s1.toString()); System.out.println(s2.toString()); fis.close(); ois.close(); }catch(Exception ex){ ex.printStackTrace(); } } }

Both the ObjectOutputStream and ObjectInputStream classes are vital components in managing serialized objects. They provide a seamless and efficient way to store objects in a file and retrieve them when needed. This capability is invaluable when there is a requirement to persistently store object data or transmit it across platforms or networks. By using these classes, developers can easily serialize and deserialize objects, allowing for efficient data manipulation and exchange in Java applications.

Conclusion

Java object serialization is a powerful mechanism that enables the conversion of objects into a byte stream, facilitating their storage and transmission. By implementing the Serializable interface and utilizing classes like ObjectOutputStream and ObjectInputStream, developers can easily serialize and deserialize objects, ensuring their persistence and seamless retrieval. Java object serialization plays a crucial role in data manipulation and exchange, providing a platform-neutral and efficient solution for storing and transmitting complex object data.