Serializing and Deserializing in Java

In this example, We will show you simple program about, How to perform serializing and deserializing in Java. This example were tested with environment and shared and also the step by step procedure explained in this post.

Project Structure

Serializing and Deserializing in Java

1. Serializing an Object in Java

The serialization is process of storing an object state into a file.

POJO Object (Customer.java)

package com.dineshkrish;
import java.io.Serializable;
/**
* 
* @author Dinesh Krishnan
*
*/
public class Customer implements Serializable {
private int customerId;
private String customerName;
public Customer(int customerId, String customerName) {
this.customerId = customerId;
this.customerName = customerName;
}
public int getCustomerId() {
return customerId;
}
public String getCustomerName() {
return customerName;
}
@Override
public String toString() {
return "Customer [customerId=" + customerId + ", customerName=" + customerName + "]";
}
}

Serializer.java

package com.dineshkrish;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
/**
* 
* @author Dinesh Krishnan
*
*/
public class Serializer {
public void serialize(File file, Object obj) {
if (obj != null && file != null) {
try (FileOutputStream fos = new FileOutputStream(file)) {
ObjectOutputStream oos = new ObjectOutputStream(fos);
// writing an object
oos.writeObject(obj);
System.out.println("The "+obj.getClass().getName()+" object serialized successfully...");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}

Example_1.java

package com.dineshkrish;
import java.io.File;
/**
* 
* @author Dinesh Krishnan
*
*/
public class Example_1 {
public static void main(String[] args) {
// file name
String fileName = "data.ser";
// creating the customer object
Customer c1 = new Customer(101, "Dinesh Krishnan");
// creating file object
File file = new File(fileName);
Serializer s = new Serializer();
// serializing an object
s.serialize(file, c1);
}
}

Output

The com.dineshkrish.Customer object serialized successfully...

2. Deserializing an Object in Java

DeSerializer.java

package com.dineshkrish;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
/**
* 
* @author Dinesh Krishnan
*
*/
public class DeSerializer<T> {
public T deserialize(File file, Class<T> type) {
T obj = null;
if(file != null) {
try(FileInputStream fis = new FileInputStream(file)) {
ObjectInputStream ois = new ObjectInputStream(fis);
// reading an object from file
obj = (T)ois.readObject();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} 
}
return obj; 
}
}

Example_2.java

package com.dineshkrish;
import java.io.File;
/**
* 
* @author Dinesh Krishnan
*
*/
public class Example_2 {
public static void main(String[] args) {
// file name
String fileName = "data.ser";
// creating file object 
File file = new File(fileName);
DeSerializer<Customer> ds = new DeSerializer<Customer>();
// deserializing an object
Customer customer = ds.deserialize(file, Customer.class);
// printing the customer object
System.out.println(customer);
}
}

Output

Customer [customerId=101, customerName=Dinesh Krishnan]

References

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *