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.18 Sequential file created using ObjectOutputStream.
|
|
1 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.ObjectOutputStream; 6 import java.util.NoSuchElementException; 7 import java.util.Scanner; 8 9 import com.deitel.jhtp6.ch14.AccountRecordSerializable; 10 11 public class CreateSequentialFile 12 { 13 private ObjectOutputStream output; 14 15 16 public void openFile() 17 { 18 try 19 { 20 output = new ObjectOutputStream( 21 new FileOutputStream( "clients.ser" ) ); 22 } 23 catch ( IOException ioException ) 24 { 25 System.err.println( "Error opening file." ); 26 } 27 } 28 29 30 public void addRecords() 31 { 32 AccountRecordSerializable record; 33 int accountNumber = 0; 34 String firstName; 35 String lastName; 36 double balance; 37 38 Scanner input = new Scanner( System.in ); 39 40 System.out.printf( "%s\n%s\n%s\n%s\n\n", 41 "To terminate input, type the end-of-file indicator ", 42 "when you are prompted to enter input.", 43 "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter", 44 "On Windows type <ctrl> z then press Enter" ); 45 46 System.out.printf( "%s\n%s", 47 "Enter account number (> 0), first name, last name and balance.", 48 "? " ); 49 50 while ( input.hasNext() ) 51 { 52 try 53 { 54 accountNumber = input.nextInt(); 55 firstName = input.next(); 56 lastName = input.next(); 57 balance = input.nextDouble(); 58 59 if ( accountNumber > 0 ) 60 { 61 62 record = new AccountRecordSerializable( accountNumber, 63 firstName, lastName, balance ); 64 output.writeObject( record ); 65 } 66 else 67 { 68 System.out.println( 69 "Account number must be greater than 0." ); 70 } 71 } 72 catch ( IOException ioException ) 73 { 74 System.err.println( "Error writing to file." ); 75 return; 76 } 77 catch ( NoSuchElementException elementException ) 78 { 79 System.err.println( "Invalid input. Please try again." ); 80 input.nextLine(); 81 } 82 83 System.out.printf( "%s %s\n%s", "Enter account number (>0),", 84 "first name, last name and balance.", "? " ); 85 } 86 } 87 88 89 public void closeFile() 90 { 91 try 92 { 93 if ( output != null ) 94 output.close(); 95 } 96 catch ( IOException ioException ) 97 { 98 System.err.println( "Error closing file." ); 99 System.exit( 1 ); 100 } 101 } 102 }
|
|
Page
1 |
2 |
3 | 4 |
5 |
6 |
7
Tutorial Index