Deitel & Associates, Inc. Logo

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

ISBN:
0-13-148398-6
© 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.21 Testing class ReadSequentialFile.
     1       // Fig. 14.21: ReadSequentialFileTest.java
  2      // This program test class ReadSequentialFile.
  3     
  4      public class ReadSequentialFileTest
  5   {
  6                     public static void main( String args[] )
  7         {
  8        ReadSequentialFile application = new ReadSequentialFile();
  9     
   10               application.openFile();
   11               application.readRecords();
   12               application.closeFile();
   13         } // end main
   14   } // end class ReadSequentialFileTest
 
Account   First Name  Last Name      Balance
100       Bob         Jones            24.98
200       Steve       Doe            -345.67
300       Pam         White             0.00
400       Sam         Stone           -42.16
500       Sue         Rich            224.62
     

    The program reads records from the file in method readRecords (lines 30–60). Line 40 calls ObjectInputStream method readObject to read an Object from the file. To use AccountRecordSerializable-specific methods, we downcast the returned Object to type AccountRecordSerializable. Method readObject throws an EOFException (processed at lines 48–51) if an attempt is made to read beyond the end of the file. Method readObject throws a ClassNotFoundException if the class for the object being read cannot be located. This might occur if the file is accessed on a computer that does not have the class. Figure 14.21 contains method main (lines 6–13), which opens the file, calls method readRecords and closes the file.

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

Tutorial Index