When you serialize an object in Java, you convert the data into groups of bytes and then convert them back to the copy of the original data. If this seems confusing, think of serialization in the following terms. You are working on a document and save a copy of it on your hard drive. You are, as it were, serializing the data so that you can retrieve a copy later. Serialization makes data transfer over the network much easier and more efficient. It is important that you understand the basics of Java before serializing an object. If you have used programming languages such as Pascal or older versions of C, you will know it without serialization, a programmer has to create a separate I / O text file to store and load data. The following article contains the steps to serialize an object in Java. The sample code in this article is used courtesy of The Java Developers Almanac 1.4.
Steps
Step 1. Open the Java encoding object that requires serialization or create one from scratch
Step 2. Select the Java object you want to serialize
In this example, we will call this object "MyObject".
Step 3. Enable object serialization in Java by making the MyObject class inherit the java.io. Serialize class
Simply add the following line of code to the beginning of the class, replacing the line "public class MyObject". Public class MyObject implements java.io. Serializable.
Step 4. Now your object is serializable, this means it can be written as an output stream, like the following:
-
The following lines of code demonstrate how to write MyObject (or any serializable object) to a file or disk.
try {
// Serialize a data object to a file
ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ("MyObject.ser"));
out.writeObject (object);
out.close ();
// Serialize an object into a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
out = new ObjectOutputStream (bos);
out.writeObject (object);
out.close ();
// Get the bytes of the serialized object
byte buf = bos.toByteArray ();
} catch (IOException e) {
}
Step 5. It can be read as follows:
try {FileInputStream door = new FileInputStream ("name_of_file.sav"); ObjectInputStream reader = new ObjectInputStream (door); MyObject x = new MyObject (); x = (MyObject) reader.nextObject ();} catch (IOException e) {e.printStackTrace ();}
Step 6. Run the serialized object code inside your Java program to make sure it actually works (optional)
Step 7. Save and close the serialized object in Java
Advice
- Serialization enhancements in Java SE Development Kit 6 allow you to use the ObjectStreamClass lookupAny method to handle all non-serializable object classes.
- To improve read and write times in a very large object tree, use the "transient" keyword to instantiate variables that do not require serialization. This will increase performance as you will no longer be reading and writing useless data in the serialization process.
Warnings
- Java offers a new version of their developer's kit roughly annually. The new releases include improvements and changes on how an Object can be serialized in Java. So it is important to monitor changes in the version you are using.
- When serializing objects, you cannot encrypt streams. Therefore, you will have to rely on other applications or a transmission process on a secondary network to protect the data if necessary.
- There is currently no option that allows you to write objects to a random access file. Instead, you can use the Byte Array input output stream as a base from which to read and write objects. However, make sure the whole object is in the Byte Array stream, otherwise the process will fail.