October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Object Serialization

  • November 7, 2005
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

This application basically does three things:

  1. Instantiates a Person object.
  2. Serializes the object.
  3. Writes the object to the file Name.txt.

The actual act of serializing and writing the object is accomplished in the following code:

oos.writeObject(person);

Now, this is obviously a lot simpler that than writing each individual attribute out one at a time. It is very convenient to simply write the object directly to the file.

It is important to know that the underlying implementation is not quite that simple. This is yet another great example of the difference between the interface and the implementation. The programmer's interface is to simply write the object to the file. You don't care how the object is actually written at the physical level. All you care about is:

  • That you can write the object as an indivisible unit
  • That you can restore the object exactly as you stored it

It is interesting to actually look at the file produced when the SavePerson application is executed. Remember that when you write data to a text file you can inspect and even edit the data. When you write a serialized object to a file, you can look at it; however, you can't (or shouldn't) edit it. Figure 3 shows what the Name.txt file looks like when opened in Notepad.

Figure 3: The Serialized Object as a Text File.

Although the format of this file is far from obvious, notice that there are some things that you can identify—for example, the name "Jack Jones".

The application SavePerson( ) writes the object to the file Name.txt. The following code restores the object.

import java.io.*;
import java.util.*;
public class RestorePerson{
   public static void main(String args[]){
      try{
         FileInputStream fis = new FileInputStream("Name.txt");
         ObjectInputStream ois = new ObjectInputStream(fis);
         Person person = (Person )ois.readObject();
         System.out.print(Person's Name Restored: ");
         System.out.println(person.getName());
         ois.close();
      }  catch(Exception e){
         e.printStackTrace();

} } }

The main line of interest here is the code that retrieves the object from the file Name.txt.

Person person = (Person )ois.readObject();

It is important to note that the object is reconstructed from the flat file and a new instance of a Person object is instantiated and initialized. This Person object is an exact replica of the Person object that you stored in the SavePerson application. Figure 4 shows the output of both the SavePerson and the RestorePerson applications.

Figure 4: Serializing an Object.

Note: The name "Jack Jones", part of the Person object, is stored in the file Name.txt when the application is executed and then the object is restored when the application RestorePerson is executed. When the object is restored, you can access the name attribute. The beauty of this approach is that all the programmer must do is save and restore the object. There is no need to deal with the individual parts of the object separately.

Conclusion

The main thrust of this article was to describe the technology of object serialization. By studying and using the code in the example above, you can save objects to a simple text file. However, the underlying theme for this article and the following articles is the idea of object wrappers. Next month, you will cover the technology of connecting to a database using JDBC. Once this is complete, you then will create useful object wrappers and design an application that will successfully separate the implementation of the data storage medium from the interfaces that you have carefully designed.

References

Gilbert, Stephen, and Bill McCarty: Object-Oriented Design in Java. The Waite Group, 1998.

Meyers, Scott: Effective C++. Addison-Wesley, 1992.

Tyma, Paul, Gabriel Torok and Troy Downing: Java Primer Plus. The Waite Group, 1996.

Ambler, Scott: The Object Primer. Cambridge University Press, 1998.

Jaworski, Jamie: Java 1.1 Developers Guide. Sams Publishing, 1997.

www.javasoft.com

About the Author

Matt Weisfeld is a faculty member at Cuyahoga Community College (Tri-C) in Cleveland, Ohio. Matt is a member of the Information Technology department, teaching programming languages such as C++, Java, and C# .NET as well as various web technologies. Prior to joining Tri-C, Matt spent 20 years in the information technology industry gaining experience in software development, project management, business development, corporate training, and part-time teaching. Matt holds an MS in computer science and an MBA in project management. Besides The Object-Oriented Thought Process, which is now in it's second edition, Matt has published two other computer books, and more than a dozen articles in magazines and journals such as Dr. Dobb's Journal, The C/C++ Users Journal, Software Development Magazine, Java Report, and the international journal Project Management. Matt has presented at conferences throughout the United States and Canada.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel