Java Reference
In-Depth Information
FileInputStream fileIn =
new
FileInputStream(
"bank.ser"
);
ObjectInputStream in =
new
ObjectInputStream( fileIn ) ;
people = (ArrayList
<
Person
>
)in.readObject();
in . close () ;
fileIn.close();
for
(Person p: people)
{
System. out . println (p) ;
}
}
}
The code first creates two customers and one employee and adds them to an
ArrayList
of people. Of course, both the
Customer
and
Employee
classes inherit from the
Person
class. The constructor of the
Person
class takes as input the name, address, and phone
number of the person. The constructor of the
Employee
class takes, in addition, the salary
of the employee. Note that since the phone number of a person is saved as a
long
,the
letter
L
is used at the end of the number. The
Address
class is a static nested class of the
Person
class. We defined it as a static nested class because the
Address
class relates to the
Person
class. It is not an inner class because there can be multiple people associated with
the same address. The constructor of the
Address
class takes as input the street number,
street name, city name, state, and zip code. Next, the code creates two bank accounts: one
for Bob and one for Ann. Note that a customer can have multiple accounts. The constructor
of the
BankAccount
class takes as input the amount of initial deposit and a reference to the
customer.
The reading and writing to/from a file happens in the second part of the code. In order to
write binary data to a file, an instance of the
FileOutputStream
class needs to be created.
The constructor of the class takes as input the name of the file where the data is to be
written. If the file could not be opened, then an exception is raised. The
FileOutputStream
class allows only binary data (i.e., an array of bytes) to be written to the file. If we want
to write objects to the file, then we need to create an object of type
ObjectOutputStream
from the
FileOutputStream
object. The
writeObject
method can be used to write objects
to a file. The above code creates the file
bank.ser
.Weusedthe
ser
file extension because
this is a common extension for serialized data.
The
writeObject
method can only write objects that belong to a class that
implements the interface
Serializable
. The interface contains no methods. The
writeObject
method is very powerful because it serializes not only the specified ob-
ject, but also its internal objects that implement the interface
Serializable
.The
serialization works even in the presence of cyclic references.
In our example, we only store the
ArrayList
of people. Since a customer is a person
and a customer can have several bank accounts, all objects of type
BankAccount
are also
stored. A bank account can contain several transactions and every transaction references a
bank account. In other words, there is a circular reference between the
BankAccount
and
Transaction
classes. However, every object of each of the two classes is stored only once.
The
writeObject
method is smart enough to determine that an object is already stored
and it does not need to be stored again. However, all objects that are stored must implement
the interface
Serializable
.
In order to make sure that we have written the data correctly, our application opens the
file a second time and prints to the screen all the data in the file. Reading objects from a
file is similar to writing objects to a file. The only difference is that a
FileInputStream
and