Lab Exercises
- Exercise 1: Serialize and deserialize an object (20 minutes)
- Exercise 2: Customizing default protocol (20 minutes)
- Exercise 3: Buffered stream (20 minutes)
- Homework Exercise (for people who are taking Sang Shin’s “Java Programming online course”)
Exercise 1: Serialize and Deserialize an object
(1.1) Serialize the current time
1. Create a new NetBeans project
- Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
- Under Choose Project pane, select Java under Categories and Java Application under Projects. Click Next.
- Under Name and Location pane, for the Project Name field, type in SerializeAndDeserializeCurrrentTime as project name.
- For Create Main Class field, type in SerializeTime.
- Click Finish.
- Observe that SerializeAndDeserializeCurrrentTime project appears and IDE generated SerializeTime.java is displayed in the source editor window of NetBeans IDE.
2. Modify the IDE generated SerializeTime.java as shown in Code-1.11 below. Study the code by paying special attention to the bold fonted parts.
import java.io.ObjectOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class SerializeTime{ public static void main(String [] args){ String filename = “time.ser”; // Create an object // Serialize the object instance and save it in System.out.println(“Current time is saved into ” + filename); |
Code-1.11: SerializeTime.java
3. Write PersistentTime.java.
import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable{ public PersistentTime() { public Date getTime() { |
InputFile-1.12: farrago.txt
3. Write DeserializeTime.java.
import java.io.ObjectInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Calendar; public class DeserializeTime { public static void main(String [] args) { String filename = “time.ser”; // Deserialize the previously saved // print out restored time // print out the current time |
InputFile-1.12: farrago.txt
4. Build and run the Serialization part of the project
- Right click SerializeTime.java project and select Run File.
- Observe the result in the Output window. (Figure-1.13 below)
Current time is saved into time.ser |
Figure-1.13: Result of running SerializeAndDeserializeCurrrentTime application
5. Build and run the Deserialization part of the project
- Right click DeserializeTime.java project and select Run File.
- Observe the result in the Output window. (Figure-1.13 below)
Previously serialized time: Mon Feb 26 01:57:16 EST 2007 Current time: Mon Feb 26 01:58:58 EST 2007 |
Figure-1.13: Result of running SerializeAndDeserializeCurrrentTime application
Solution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as <LAB_UNZIPPED_DIRECTORY>/javaserialization/samples/SerializeAndDeserializeCurrrentTime. You can just open it and run it.
5. For your own exercise, do the following tasks. Build and run the application.
- Add another field called myName which is String type to the PersistentTIme class.
- Modify SerializeTime.java and DeserializeTime.java to display the myName field.
(1.2) Use transient keyword
import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable{ public PersistentTime() { public Date getTime() { |
Code-1.11: SerializeTime.java
2. Build and run the Serialization part of the project
- Right click SerializeTime.java project and select Run File.
- Observe the result in the Output window. (Figure-1.13 below)
Current time is saved into time.ser |
Figure-1.13: Result of running SerializeAndDeserializeCurrrentTime application
3. Build and run the Deserialization part of the project
- Right click DeserializeTime.java project and select Run File.
- Observe the result in the Output window. (Figure-1.13 below)
Previously serialized time: null Current time: Mon Feb 26 02:07:09 EST 2007 |
Figure-1.13: Result of running SerializeAndDeserializeCurrrentTime application
Solution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as <LAB_UNZIPPED_DIRECTORY>/javaserialization/samples/SerializeAndDeserializeCurrrentTimeTransient. You can just open it and run it.
4. For your own exercise, do the following tasks. Build and run the application.
- Make myName field transient.
- Object that the field is not serialized.
Summary
Exercise 2: Version Control
(2.1) Modify the class after serialization
- Right click SerializeTime.java project and select Run File.
- Observe the result in the Output window. (Figure-2.10 below)
Current time is saved into time.ser |
Figure-2.10: Result
2. Modify the PersistentTim.java as shown in Code-2.11 below. The change is to add another field to the PersistentTime class after the serialization.
import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable{ private Date time; public PersistentTime() { public Date getTime() { |
Code-2.11: FileReaderWriter.java
3. Compile the PersistentTime.java.
- Right click PersistentTime.java and select Compile File.
4. Build and run the Deserialization part of the project
- Right click DeserializeTime.java project and select Run File.
- Observe the java.io.InvalidClassException exception in the Output window. (Figure-2.12 below)
java.io.InvalidClassException: PersistentTime; local class incompatible: stream classdesc serialVersionUID = -3126998878902358585, local class serialVersionUID = -5560460247034149373 at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:519) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1546) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1460) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1693) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1299) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339) at DeserializeTime.main(DeserializeTime.java:23) Exception in thread “main” java.lang.NullPointerException |
Figure-2.12: java.io.InvalidClassException exception
(2.2) Use a version id
import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable{ static final long serialVersionUID = -3126998878902358585L; private Date time; public PersistentTime() { public Date getTime() { |
Code-2.21: Assign a unique version id to the class file
2. Compile the PersistentTime.java.
- Right click PersistentTime.java and select Compile File.
3. Build and run the Serialization part of the project.
- Right click SerializeTime.java project and select Run File.
- Observe the result in the Output window. (Figure-1.13 below)
Current time is saved into time.ser |
Figure-1.13: Result of running SerializeAndDeserializeCurrrentTime application
4. Modify the PersistentTim.java as shown in Code-2.11 below. The change is to add another field to the PersistentTime class after the serialization.
import java.io.Serializable; import java.util.Date; import java.util.Calendar; public class PersistentTime implements Serializable{ private Date time; public PersistentTime() { public Date getTime() { |
Code-2.11: FileReaderWriter.java
5. Compile the PersistentTime.java.
- Right click PersistentTime.java and select Compile File.
6. Build and run the Deserialization part of the project
- Right click DeserializeTime.java project and select Run File.
- Observe the result in the Output window. (Figure-2.23 below)
Previously serialized time: Mon Feb 26 03:04:59 EST 2007 Current time: Mon Feb 26 03:05:13 EST 2007 |
Figure-2.23: Result
Solution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as <LAB_UNZIPPED_DIRECTORY>/javaserialization/samples/SerializeAndDeserializeCurrrentTimeVersionControl. You can just open it and run it.
Summary
In this exercise, you have learned how to do version control for serialization and deserialization of an object.
Exercise 3: Customizing default protocol
In this exercise, you will learn how to provide your own readObject() and writeObject() methods, thus changing the behavior of the default protocol.
(3.1) Using the default readObject() and writeObject()
- Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
- Under Choose Project pane, select Java under Categories and Java Application under Projects. Click Next.
- Under Name and Location pane, for the Project Name field, type in SerializeAnimationThreadNotStarted as project name.
- For Create Main Class field, type in SerializeAnimationThreadNotStarted.
- Click Finish.
- Observe that SerializeAnimationThreadNotStarted project appears and IDE generated SerializeAnimationThreadNotStarted.java is displayed in the source editor window of NetBeans IDE.
2. Modify the IDE generated SerializeAnimationThreadNotStarted.java as shown in Code-3.11 below. Study the code by paying special attention to the bold fonted parts.
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializeAnimationThreadNotStarted { public static void main(String[] args) { // Create an object instance // Serialize the object // Deserialize the object. The problem is that the PersistentAnimation b = null; } } |
Code-3.11: SerializeAnimationThreadNotStarted.java
3. Write PersistentAnimation.java as shown in Code-3.12 below. Study the code by paying special attention to the bold fonted parts.
import java.io.Serializable;
public class PersistentAnimation implements Serializable, Runnable { transient private Thread animator; public PersistentAnimation(int animationSpeed) { public void run() { |
Code-3.12: PersistentAnimation.java
4. Build and run the project
- Right click SerializeAnimationThreadNotStarted project and select Run Project.
- Observe the result in the Output window. (Figure-3.13 below) Observe that the thread is not started automatically when the object is deserialized.
PersistentAnimation thread is started |
Figure-3.13: Result of running SerializeAnimationThreadNotStarted application
Solution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as <LAB_UNZIPPED_DIRECTORY>/javaserialization/samples/SerializeAnimationThreadNotStarted. You can just open it and run it.
(3.2) Using your own readObject() and writeObject()
import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class PersistentAnimation implements Serializable, Runnable { transient private Thread animator; public PersistentAnimation(int animationSpeed) { public void run() { // Provide your own writeObject method // Provide your own readObject method } private void startAnimation() { |
Code-3.21: PersistentAnimation.java
2. Build and run the project
- Right click SerializeAnimationThreadStarted project and select Run Project.
- Observe the result in the Output window. (Figure-3.22 below) Observe that the thread is now started when the object is deserialized.
PersistentAnimation thread is started PersistentAnimation thread is started |
Figure-3.22: Result of running SerializeAnimationThreadStarted application
Solution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as <LAB_UNZIPPED_DIRECTORY>/javaserialization/samples/SerializeAnimationThreadStarted. You can just open it and run it.
Summary
Homework exercise (for people who are taking Sang Shin’s “Java Programming online course”)
- Write MyClassToBePersisted.java, which contains the following properties
- Profile field, which has the following fields
- String name
- int age
- String hobby
- School field, which has the following fields
- String nameOfSchool
- int yearStarted (this is transient field)
- Write SerializeMyClassToBePersisted.java which creates an instance of MyClassToBePersisted class and serialize it into a file in its main method.
- Write DeserializeMyClassToBePersisted.java which read the serialized file and deserialize it into instance of MyClassToBePersisted class in its main method.
- Profile field, which has the following fields
- Write MyClassToBePersisted.java, which contains the following properties with version control.
- Zip file of the the MySerializationProject NetBeans project. (Someone else should be able to open and run it as a NetBeans project.) You can use your favorite zip utility or you can use “jar” utility that comes with JDK as following.
- cd <parent directory that contains MySerializationProject directory> (assuming you named your project as MySerializationProject)
- jar cvf MySerializationProject.zip MySerializationProject (MySerializationProject should contain nbproject directory)
- Captured output screen – name it as JavaIntro-javaserialization.gif or JavaIntro-javaserialization.jpg (or JavaIntro-javaserialization.<whatver graphics format>)
- Any screen capture that shows that your program is working is good enough. No cosmetic polishment is required.
- If you decide to use different IDE other than NetBeans, the zip file should contain all the files that are needed for rebuilding the project.