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.18 Sequential file created using ObjectOutputStream.
   1  // Fig. 14.18: CreateSequentialFile.java
2
// Writing objects sequentially to a file with class ObjectOutputStream.
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; // outputs data to file
14

15
// allow user to specify file name
16
public void openFile()
17
{
18
try // open file
19
{
20
output = new ObjectOutputStream(
21
new FileOutputStream( "clients.ser" ) );
22
} // end try
23
catch ( IOException ioException )
24
{
25
System.err.println( "Error opening file." );
26
} // end catch
27
} // end method openFile
28

29
// add records to file
30
public void addRecords()
31
{
32
AccountRecordSerializable record; // object to be written to file
33
int accountNumber = 0; // account number for record object
34
String firstName; // first name for record object
35
String lastName; // last name for record object
36
double balance; // balance for record object
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() ) // loop until end-of-file indicator
51
{
52
try // output values to file
53
{
54
accountNumber = input.nextInt(); // read account number
55
firstName = input.next(); // read first name
56
lastName = input.next(); // read last name
57
balance = input.nextDouble(); // read balance
58

59
if ( accountNumber > 0 )
60
{
61
// create new record
62
record = new AccountRecordSerializable( accountNumber,
63
firstName, lastName, balance );
64
output.writeObject( record ); // output record
65
} // end if
66
else
67
{
68
System.out.println(
69
"Account number must be greater than 0." );
70
} // end else
71
} // end try
72
catch ( IOException ioException )
73
{
74
System.err.println( "Error writing to file." );
75
return;
76
} // end catch
77
catch ( NoSuchElementException elementException )
78
{
79
System.err.println( "Invalid input. Please try again." );
80
input.nextLine(); // discard input so user can try again
81
} // end catch
82

83
System.out.printf( "%s %s\n%s", "Enter account number (>0),",
84
"first name, last name and balance.", "? " );
85
} // end while
86
} // end method addRecords
87

88
// close file and terminate application
89
public void closeFile()
90
{
91
try // close file
92
{
93
if ( output != null )
94
output.close();
95
} // end try
96
catch ( IOException ioException )
97
{
98
System.err.println( "Error closing file." );
99
System.exit( 1 );
100 } // end catch
101 } // end method closeFile
102 } // end class CreateSequentialFile
Page 1 | 2 | 3 | 4 | 5 | 6 | 7

Tutorial Index