26
Feb

Java Serialization

Serialization / Deserialization in java
=========================================

Step 1:  Define the class, whose object needs to be serialized/deserialized from file system
==============================================================================================


public class Student implements Serializable {

	private static final long serialVersionUID = 6153364608881636837L;
	private int id;
	private String name;

	public Student(int i, String n) {
		this.id = i;
		this.name = n;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}
}


Step 2: Use following code to serialize / deserialize contents 
=================================================================

package J8Features;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerDeserialization {

	public static void main(String[] args) {
		Student obj = new Student(21, "Jacky");

		File file = new File("C:\\s.ser");

		// Serialization
		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
			oos.writeObject(obj);
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Deserialization
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
			Student obj2 = (Student) ois.readObject();
			System.out.println(obj2.getId() + "::" + obj2.getName());
			ois.close();
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

}



Output:
21::Jacky




Serializing/Deserializing multiple objects (of same class)
=============================================================

package J8Features;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class SerDeserialization {

	public static void main(String[] args) {
		Student obj1 = new Student(21, "Jacky1");
		Student obj2 = new Student(22, "Jacky2");
		Student obj3 = new Student(23, "Jacky3");
		Student obj4 = new Student(24, "Jacky4");
		Student obj5 = new Student(25, "Jacky5");
		Student obj6 = new Student(26, "Jacky6");

		List<Student> list = new ArrayList<>();
		list.add(obj1);
		list.add(obj2);
		list.add(obj3);
		list.add(obj4);
		list.add(obj5);
		list.add(obj6);
		File file = new File("C:\\s.ser");

		// Serialization
		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
			oos.writeObject(list);
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Deserialization
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
			List<Student> listR = (List<Student>) ois.readObject();
			for (Student s : listR)
				System.out.println(s.getId() + "::" + s.getName());
			ois.close();
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

}


Output:
21::Jacky1
22::Jacky2
23::Jacky3
24::Jacky4
25::Jacky5
26::Jacky6