Chapter 14

Harshana Samarasinghe
CodeX
Published in
4 min readApr 25, 2022

Serialization and File I/O

-Capture the Pattern-

If your data is simply going to be used by the Java application that created it:

01. Use serialization

Create a file with flattened (serialized) objects in it. Then have your software read the serialized objects from the file and inflate them back into heap-inhabiting objects.

If your data will be used by other programs:

02.Write a plain text file

Create a file with delimiters that can be parsed by other applications. For instance, a tab-delimited file that may be read by a spreadsheet or database program.

Writing a serialized object to a file

1. Make a File Output Stream

2.Make an Object Output Stream

3.Write the object

4.Close the Object Output Stream

Data moves in streams from one place to another.
  • Objects in a heap have a state and a value for the object’s instance variable. As a result, instances of the same class are different.
  • The value of instance variables is saved in serialized objects. As a result, equal objects can be added to the heap.
  • When an item is serialized, it refers to objects that are stored in the instance variables. Those objects, as well as the objects to which they refer, are both serialized.

We can create a class serializable by implementing serializable in it.

If a superclass is serializable, the subclasses will be serialized or implement serializable automatically.

  • If the state of certain objects was not stored properly because the full object graph was serialized correctly or serialization failed.
  • If you can’t save an instance variable because it’s not serializable, use the transitory keyword. By using a serializable process, you may avoid having to deal with that variable.

Deserialization: restoring an object

1.Make a File Input Stream

2.Make an Object Input Stream

3.Read the objects

4.Cast the objects

5.Close the Object Input Stream

When an object is deserialized, the JVM attempts to re-serialize it by creating a new object on the heap with the same state as the original serialized object.

1. Object is read from stream

2. JVM determine object class type

3. Then it try to find and load object class if not then jvm throws an exception

4. A new object is given space on the heap.

5. If an object has a non serializable class in the inheritance tree then the constructor for that will run along with the constructors that above it. After constructor chaining happens then all super classes which are non serialized will reinitialize their state.

6. Object instance variables are given the value from the serialized state.

Static variables are not serialized.

-The java.io.File class-

Quiz Card Builder (code outline)

The file on the disk is represented by the Java.io.file class. It represents the file directory’s name and path, and it prevents access to the data in that file. The following are some of the things we can perform with a file object.

Writing a String to a Text File

1.Make a File object representing an existing file

2.Make a new directory

3.List the contents of a directory

4.Get the absolute path of a file or directory

5.Delete a file or directory (returns true if successful)

  • We may start reading a text file by using a file reader connection stream.
  • To split a string into separate tokens, use the String split() function.
  • When an object is serialized, the object’s class is assigned a version id number. Serial Version UID is the name of this id.

--

--

Harshana Samarasinghe
CodeX
Writer for

“Truth can only be found in one place: the code.” - Associate Engineer — Java Technology at Virtusa Sri Lanka -