Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

The following Java tutorial introduces object serialization--Java's built-in mechanism for manipulating objects as streams of bytes. Object serialization provides the foundation for Java's remote method invocation (RMI) capabilties that enable Java programs that are distributed over a network to invoke each others so-called "remote methods." RMI is used frequently in distributed enterprise applications that are built with Java Enterprise Edition (Java EE). In this tutorial, we demonstrate object serialization by writing entire objects to and reading entire objects from files on disk. This tutorial is intended for students and professionals who are already familiar with Java programming. Download the examples for this tutorial here.

[Notes: This tutorial is an excerpt (Section 14.6) of Chapter 14, Files and Streams, from our best-selling textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. When you purchase this book you also get free access to the Web-based Java Multimedia Cyber Classroom, 6/e, for six months. The Cyber Classroom includes audio descriptions of the examples in Chapters 1-14, solutions to approximately one-half of the end-of-chapter exercises, interactive true/false self-assessment questions and a searchable Web-based e-book. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp. 697-706. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

Introduction to Object Serialization (Continued)

Fig. 14.19 Testing class CreateSequentialFile.
1 // Fig. 14.19:
// Testing class CreateSequentialFile.

public class CreateSequentialFileTest
public static void main( String args[] )
CreateSequentialFile application = new CreateSequentialFile();

} // end main
} // end class CreateSequentialFileTest
To terminate input, type the end-of-file indicator
when you are prompted to enter input.
On UNIX/Linux/Mac OS X type <ctrl> d then press Enter
On Windows type <ctrl> z then press Enter

Enter account number (> 0), first name, last name and balance.
? 100 Bob Jones 24.98
Enter account number (> 0), first name, last name and balance.
? 200 Steve Doe -345.67
Enter account number (> 0), first name, last name and balance.
? 300 Pam White 0.00
Enter account number (> 0), first name, last name and balance.
? 400 Sam Stone -42.16
Enter account number (> 0), first name, last name and balance.
? 500 Sue Rich 224.62
Enter account number (> 0), first name, last name and balance.
? ^Z

Common Programming Error 14.2
It is a logic error to open an existing file for output when, in fact, the user wishes to preserve the file.

    Class FileOutputStream provides methods for writing byte arrays and individual bytes to a file. In this program we wish to write objects to a file—a capability not provided by FileOutputStream. For this reason, we wrap a FileOutputStream in an ObjectOutputStream by passing the new FileOutputStream object to the ObjectOutputStream’s constructor (lines 20–21). The ObjectOutputStream object uses the FileOutputStream object to write objects into the file. Lines 20–21 might throw an IOException if a problem occurs while opening the file (e.g., when a file is opened for writing on a drive with insufficient space or when a read-only file is opened for writing). If so, the program displays an error message (lines 23–26). If no exception occurs, the file is open and variable output can be used to write objects to the file.

    This program assumes that data is input correctly and in the proper record-number order. Method addRecords (lines 30–86) performs the write operation. Lines 62–63 create an AccountRecordSerializable object from the data entered by the user. Line 64 calls ObjectOutputStream method writeObject to write the record object to the output file. Note that only one statement is required to write the entire object.

    Method closeFile (lines 89–101) closes the file. Method closeFile calls ObjectOutputStream method close on output to close both the ObjectOutputStream and its underlying FileOutputStream (line 94). Note that the call to method close is contained in a try block. Method close throws an IOException if the file cannot be closed properly. In this case, it is important to notify the user that the information in the file might be corrupted. When using wrapped streams, closing the outermost stream also closes the underlying file.

    In the sample execution for the program in Fig. 14.19, we entered information for five accounts. The program does not show how the data records actually appear in the file. Remember that now we are using binary files, which are not humanly readable. To verify that the file has been created successfully, the next section presents a program to read the file’s contents.

Page 1 | 2 | 3 | 4 | 5 | 6 | 7

Tutorial Index