Now that we’ve learned about the Serializable
interface and how to implement it, let’s take a look at how to serialize an object to a file. To do this we’ll need to use the helper classes, FileOutputStream
, which will help us write to a file, and ObjectOutputStream
, which will help us write a serializable object to an output stream.
Let’s look at how to do this with some code:
public class Person implements Serializable { private String name; private int age; private static final long serialVersionUID = 1L; public Person(String name, int age) { this.name = name; this.age = age; } public static void main(String[] args) throws FileNotFoundException, IOException{ Person michael = new Person("Michael", 26); Person peter = new Person("Peter", 37); FileOutputStream fileOutputStream = new FileOutputStream("persons.txt"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(michael); objectOutputStream.writeObject(peter); } }
In the example above we:
- Added a constructor to the
Person
class we defined in the previous lesson. - Defined
main()
and initialized twoPerson
objects -michael
andpeter
. - Initialized a
FileOutputStream
object inmain()
which will create and write a stream of bytes to a file named person-file.txt. - Initialized an
ObjectOutputStream
object inmain()
which will help serialize an object to a specified output stream. - Used
objectOutputStream.writeObject()
inmain()
to serialize themichael
andpeter
objects to a file.
After the execution of the above program, the persons.txt will contain a stream of bytes that has the type and value information of the fields for the michael
and peter
objects respectively.
Let’s practice serializing an object to a file.
Instructions
Let’s define a constructor above main()
for the Car class
that we’ll use to create Car
objects.
Create a public
constructor with two parameters, a String
and int
type named make
and year
respectively. Initialize the member fields make
and year
to the parameters make
and year
in the constructor.
In the main()
method, let’s create all the objects we need in order to serialize an object to a file. Create:
- A
Car
object namedtoyota
with the constructor defined previously, passing theString "Toyota"
and theint 2021
as arguments. - A
Car
object namedhonda
with the constructor defined previously, passing theString "Honda"
and theint 2020
as arguments. - A
FileOutputStream
object (this class has been imported for you) namedfileOutputStream
, passing theString "cars.txt"
as an argument. This will be the name of the file created. - A
ObjectOutputStream
object (this class has been imported for you) namedobjectOutputStream
, passingfileOutputStream
to its constructor.
Now that we have all our helper objects, let’s serialize the Car
objects created to the cars.txt file.
In the main()
, after all the object declarations we did previously, serialize the Car
objects toyota
and honda
using the writeObject()
method of objectOutputStream
.
We should see the file cars.txt appear in the file navigator. If we open the file, we see strange text written to it. This strange text is our serialized objects!