How to Use Serialized Objects in Java

  • google plus

In Brief...

What if you created an object in Java and wanted to make it permanent so you can continue working on the object at a later time? The solution is to serialize the object and then write it to a file. When you need the object in the future, you can then read the object back into memory from the file.

Java provides several classes in the java.io package that assist the developer in using serialized objects. To learn how to use serialized objects in Java, follow these seven steps.

Take our Introduction to Java Training course for free.

See the Course Outline and Register

Instructions

  1. First, you will create the class from which the serialized object will be created. Open your text editor and type in the following Java statements:
    Java Source for Serialized Person
    The Person class must implement java.io.Serializable in order to be serialized.
  2. Save your file as Person.java.
  3. Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter.
    Compile Serialized Person
  4. Now you will create the program that serializes the Person class. Open your text editor and type in the following Java statements:
    Java Source for Serialized Objects
    The program instantiates the Person object that will be serialized. Next, an OutputObjectStream object is created. When you code the constructor argument of FileOutputStream and FileInputStream, replace the string containing the path ("c:/JavaStuff") with a directory that exists on your computer. You can retain the file name (Person.ser) or rename it if you prefer. The ObjectOutputStream constructor is placed in a "try with resources" statement. Therefore the file will be closed regardless of whether an exception occurs or not. The IOException is possible during execution of the FileOutputStream constructor. The program writes the Person object to the file using the printObject method. The ObjectInputStream constructor is placed in a "try with resources" statement. Therefore the file will be closed regardless of whether an exception occurs or not. The FileNotFoundException is possible during execution of the FileInputStream constructor. The ClassNotFoundException is possible when calling the readObject. Therefore, a generic catch is provided for both exceptions. The program reads the serialized object using the readObject method and then displays the contents of the object that is read, i.e., the full name of the person.
  5. Save your file as UseSerializedObjects.java.
  6. Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter.
    Compile Source for Serialized Objects Tester
  7. Type in the command to run your program and hit Enter.
    Run Serialized Objects Tester
    The output displays the full name stored in the serialized object and then the full name in the retrieved object. The name is identical, verifying that the object was successfully stored in the file.

Author: Stephen Withrow

Stephen has over 30 years' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.

Discuss