Java

Java Object Serialization

Let’s see how we can serialize a customer object to a file and read it back again.

Customer Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package serialization;
 
import java.io.Serializable;
 
/**
 * represents a customer
 * @author George Karpouzas
 */
public class Customer implements Serializable{
    private String _firstname, _lastname;
 
    /**
     * default constructor
     */
    public Customer(){
        this._firstname = "";
        this._lastname = "";
    }
 
    /**
     * constructor specifying firstname and lastname
     * @param firstname
     * @param lastname 
     */
    public Customer(String firstname, String lastname){
        this._firstname = firstname;
        this._lastname = lastname;
    }
 
    /**
     * set first name
     * @param value 
     */
    public void setFirstname(String value){
        this._firstname = value;
    }
 
    /**
     * set last name
     * @param value 
     */
    public void setLastname(String value){
        this._lastname = value;
    }
 
    /**
     * get first name
     * @return String
     */
    public String getFirstname(){
        return this._firstname;
    }
 
    /**
     * get last name
     * @return String
     */
    public String getLastname(){
        return this._lastname;
    }
 
    /**
     * to string!
     */
    @Override
    public String toString(){
        return this._lastname + " " + this._firstname;
    }
}

Serializer Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package serialization;
 
import java.io.*;
 
/**
 * reads customer from file
 * @author George Karpouzas
 */
public class Serializer {
    /**
     * read searialized customer
     * @return Customer
     * @see Customer
     */
    public static Customer read(String filename){
        Customer temp;
        try {
            FileInputStream fis = new FileInputStream(filename);
            try (ObjectInputStream in = new ObjectInputStream(fis)) {
                temp = (Customer) in.readObject();
            }
            return temp;
        } catch (IOException | ClassNotFoundException ex) { System.err.println(ex.getMessage()); return null; }
    }
 
    /**
     * write customer to file
     * @param customer
     * @param filename 
     * @see Customer
     */
    public static void write(Customer customer, String filename){
        try {
            FileOutputStream fos = new FileOutputStream(filename);
            try (ObjectOutputStream out = new ObjectOutputStream(fos)) {
                out.writeObject(customer);
            }
        } catch (IOException ex) { System.err.println(ex.getMessage()); }
    }
}

Usage

1
2
3
4
5
6
7
8
9
10
11
package serialization;
 
public class Serialization {
 
    public static void main(String[] args) {
        Customer cust = new Customer("George", "Karpouzas");
        Serializer.write(cust, "/path/to/files/gk.customer");
        cust = Serializer.read("/path/to/files/gk.customer");
        System.out.println(cust);
    }
}