Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.
After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.
How to Create a Simple In Memory Cache in Java (Lightweight Cache)
Classes ObjectInputStream
and ObjectOutputStream
are high-level streams that contain the methods for serializing and deserializing an object.
Below tutorial will work also if you have any of below questions:
- How to serialize and deserialize an object using JSON
- How to serialize and deserialize an object in java example
- Java serialize deserialize object to xml string
- Serialize and deserialize a binary tree
- Serialize list in Java
Here is a complete example. These are the steps:
- Create Class Item() which implements Serializable.
- In Main – Create 2 Item Objects.
- Add it to ArrayList.
- Serialize the ArrayList. Checkout file to see bytestream of an Object. (Below image)
- Deserialize the bytestream from the same file to see Object.
package com.crunchify.tutorials; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; @SuppressWarnings("serial") public class CrunchifySerializeDeserialize implements Serializable { public static void main(String[] args) throws ClassNotFoundException { int i; Item[] items = new Item[2]; CrunchifySerializeDeserialize c = new CrunchifySerializeDeserialize(); for (i = 0; i < items.length; i++) { items[i] = c.new Item(); // create array } // hard-coded values of id, desc, cost, qty items[0].setItemID("ITEM101"); items[1].setItemID("ITEM102"); items[0].setDesc("iPad"); items[1].setDesc("iPhone"); items[0].setCost(499); items[1].setCost(599); items[0].setQuantity(1); items[1].setQuantity(3); System.out.println("Item Details....."); for (Item d : items) { System.out.print(d.getItemID()); System.out.print("\t" + d.getDesc()); System.out.print("\t" + d.getCost()); System.out.println("\t" + d.getQuantity()); } List<Item> obj; obj = new ArrayList<Item>(); for (i = 0; i < items.length; i++) { obj.add(items[i]); } // Let's serialize an Object try { FileOutputStream fileOut = new FileOutputStream("/Users/<UserName>/Downloads/CrunchifyTest/Crunchify_Test1.txt"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(obj); out.close(); fileOut.close(); System.out.println("\nSerialization Successful... Checkout your specified output file..\n"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // Let's deserialize an Object try { FileInputStream fileIn = new FileInputStream("/Users/<UserName>/Downloads/CrunchifyTest/Crunchify_Test1.txt"); ObjectInputStream in = new ObjectInputStream(fileIn); System.out.println("Deserialized Data: \n" + in.readObject().toString()); in.close(); fileIn.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public class Item implements Serializable { private String itemID; private String desc; private double cost; private int quantity; public Item() { itemID = ""; desc = ""; cost = 0; quantity = 0; } public Item(String id, String d, double c, int q) { itemID = id; desc = d; cost = c; quantity = q; } /** * @return the itemID */ public String getItemID() { return itemID; } /** * @param itemID * the itemID to set */ public void setItemID(String itemID) { this.itemID = itemID; } /** * @return the desc */ public String getDesc() { return desc; } /** * @param desc * the desc to set */ public void setDesc(String desc) { this.desc = desc; } /** * @return the cost */ public double getCost() { return cost; } /** * @param cost * the cost to set */ public void setCost(double cost) { this.cost = cost; } /** * @return the quantity */ public int getQuantity() { return quantity; } /** * @param quantity * the quantity to set */ public void setQuantity(int quantity) { this.quantity = quantity; } /* * @see java.lang.Object#toString() */ @Override public String toString() { return "Item [itemID=" + itemID + ", desc=" + desc + ", cost=" + cost + ", quantity=" + quantity + "]"; } } }
Output:
Item Details..... ITEM101 iPad 499.0 1 ITEM102 iPhone 599.0 3 Serialization Successful... Checkout your specified output file.. Deserialized Data: [Item [itemID=ITEM101, desc=iPad, cost=499.0, quantity=1], Item [itemID=ITEM102, desc=iPhone, cost=599.0, quantity=3]]
List of all Java Tutorials and Spring MVC Tutorials which you might be interested in.