|
| 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 | +} |
0 commit comments