Skip to content

Commit 49d36b9

Browse files
another serialization example
1 parent 0cdd340 commit 49d36b9

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package learnHeadFirstJava.another_serialization_example;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.io.ObjectInputStream;
6+
7+
public class DeseriliazeDemo {
8+
private static final String FILE_DIRECTORY = "/tmp/employee.ser";
9+
static Employee e = null;
10+
public static void main(String[] args) {
11+
try {
12+
FileInputStream fis = new FileInputStream(FILE_DIRECTORY);
13+
ObjectInputStream ois = new ObjectInputStream(fis);
14+
e = (Employee) ois.readObject();
15+
ois.close();
16+
fis.close();
17+
} catch (IOException e) {
18+
System.out.println("Caught IOException");
19+
e.printStackTrace();
20+
return;
21+
} catch (ClassNotFoundException e) {
22+
System.out.println("Employee class not found");
23+
e.printStackTrace();
24+
return;
25+
}
26+
27+
System.out.println("Deserialized employee.ser is: " + e.toString() + ".");
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package learnHeadFirstJava.another_serialization_example;
2+
3+
import java.io.Serializable;
4+
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.stereotype.Repository;
7+
import org.springframework.stereotype.Service;
8+
9+
import lombok.Getter;
10+
import lombok.RequiredArgsConstructor;
11+
import lombok.Setter;
12+
13+
//Any one of the following three annotations: @Service, @Repository and @Component will work fine here since I'm doing a component-scan.
14+
//@Component
15+
//@Repository
16+
@Service
17+
@RequiredArgsConstructor
18+
public class Employee implements Serializable {
19+
@Setter
20+
@Getter
21+
private String address;
22+
@Setter
23+
@Getter
24+
private String name;
25+
@Setter
26+
@Getter
27+
private int age;
28+
@Setter
29+
@Getter
30+
private double salary;
31+
@Setter
32+
@Getter
33+
private transient int SSN;// any field that cannot be serialized needs to be
34+
// marked as transient, for demo purpose, we
35+
// just set SSN as a non-serializable field, it
36+
// doesn't mean anything, it's just that we
37+
// don't want to serialize it.
38+
39+
public void mailCheck() {
40+
System.out.println("Mailing a check to " + name + " at " + address);
41+
}
42+
43+
@Override
44+
public String toString() {//this toString() method doesn't contain SSN field either.
45+
return "Employee [address=" + address + ", name=" + name + ", age="
46+
+ age + ", salary=" + salary + "]";
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package learnHeadFirstJava.another_serialization_example;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.ObjectInputStream;
7+
import java.io.ObjectOutputStream;
8+
9+
import org.springframework.context.support.AbstractApplicationContext;
10+
import org.springframework.context.support.ClassPathXmlApplicationContext;
11+
12+
/**
13+
* This is a combo class that combines the other two classes together:
14+
* SerializeDemo and DeserializeDemo, makes it easier to read and play with.
15+
*/
16+
public class MainApp {
17+
private static final String FILE_DIRECTORY = "/tmp/employee.ser";
18+
19+
public static void main(String[] args) {
20+
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
21+
"spring-configuration/serialization-spring-configuration.xml");
22+
Employee employee = (Employee) context.getBean("employee");
23+
24+
// Employee employee = new Employee();//I don't need this line any more,
25+
// since I quickly set up a component-scan using Spring to auto wire
26+
// this employee bean for me, this is so cool! I feel so at home with
27+
// Spring dependency injection now! Praise the Lord!a
28+
employee.setName("Steve Sun");
29+
employee.setAge(26);
30+
employee.setAddress("1860 Charmeran Ave, San Jose, USA. 2015/08/08");
31+
employee.setSSN(12345678);
32+
employee.setSalary(103000);
33+
34+
try {
35+
FileOutputStream fos = new FileOutputStream(FILE_DIRECTORY);
36+
ObjectOutputStream ous = new ObjectOutputStream(fos);
37+
ous.writeObject(employee);
38+
ous.close();
39+
fos.close();
40+
System.out.println("Serialized data is saved in " + FILE_DIRECTORY
41+
+ ".");
42+
} catch (IOException e) {
43+
e.printStackTrace();
44+
}
45+
46+
Employee emp = null;
47+
48+
try {
49+
FileInputStream fis = new FileInputStream(FILE_DIRECTORY);
50+
ObjectInputStream ois = new ObjectInputStream(fis);
51+
emp = (Employee) ois.readObject();
52+
ois.close();
53+
fis.close();
54+
} catch (IOException e) {
55+
System.out.println("Caught IOException");
56+
e.printStackTrace();
57+
return;
58+
} catch (ClassNotFoundException e) {
59+
System.out.println("Employee class not found");
60+
e.printStackTrace();
61+
return;
62+
}
63+
64+
System.out.println("Deserialized employee.ser is: " + emp.toString()
65+
+ ".");
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package learnHeadFirstJava.another_serialization_example;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
import java.io.ObjectOutputStream;
6+
7+
import org.springframework.context.support.AbstractApplicationContext;
8+
import org.springframework.context.support.ClassPathXmlApplicationContext;
9+
10+
/**
11+
* Java provides a mechanism, called object serialization where an object can be
12+
* represented as a sequence of bytes that includes the object's data as well as
13+
* information about the object's type and the types of data stored in the
14+
* object.
15+
*
16+
* After a serialized object has been written into a file, it can be read from
17+
* the file and deserialized that is, the type information and bytes that
18+
* represent the object and its data can be used to recreate the object in
19+
* memory.
20+
*
21+
* Most impressive is that the entire process is JVM independent, meaning an
22+
* object can be serialized on one platform and deserialized on an entirely
23+
* different platform.
24+
*
25+
* Classes ObjectInputStream and ObjectOutputStream are high-level streams that
26+
* contain the methods for serializing and deserializing an object.
27+
*
28+
* The ObjectOutputStream class contains many write methods for writing various
29+
* data types, but one method in particular stands out:
30+
*
31+
* public final void writeObject(Object x) throws IOException
32+
*
33+
* The above method serializes an Object and sends it to the output stream.
34+
* Similarly, the ObjectInputStream class contains the following method for
35+
* deserializing an object:
36+
*
37+
* public final Object readObject() throws IOException, ClassNotFoundException
38+
*
39+
* This method retrieves the next Object out of the stream and deserializes it.
40+
* The return value is Object, so you will need to cast it to its appropriate
41+
* data type.
42+
*/
43+
44+
public class SerializeDemo {
45+
46+
private static final String FILE_DIRECTORY = "/tmp/employee.ser";
47+
48+
public static void main(String[] args) {
49+
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
50+
"spring-configuration/serialization-spring-configuration.xml");
51+
Employee employee = (Employee) context.getBean("employee");
52+
53+
// Employee employee = new Employee();//I don't need this line any more,
54+
// since I quickly set up a component-scan using Spring to auto wire
55+
// this employee bean for me, this is so cool! I feel so at home with
56+
// Spring dependency injection now! Praise the Lord!a
57+
employee.setName("Steve Sun");
58+
employee.setAge(26);
59+
employee.setAddress("1860 Charmeran Ave, San Jose, USA.");
60+
employee.setSSN(12345678);
61+
employee.setSalary(103000);
62+
63+
try {
64+
FileOutputStream fos = new FileOutputStream(FILE_DIRECTORY);
65+
ObjectOutputStream ous = new ObjectOutputStream(fos);
66+
ous.writeObject(employee);
67+
ous.close();
68+
fos.close();
69+
System.out.println("Serialized data is saved in " + FILE_DIRECTORY
70+
+ ".");
71+
} catch (IOException e) {
72+
e.printStackTrace();
73+
}
74+
}
75+
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beans xmlns="http://www.springframework.org/schema/beans"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
10+
11+
<context:component-scan base-package="learnHeadFirstJava.another_serialization_example"/>
12+
13+
</beans>

0 commit comments

Comments
 (0)