20.3. Stacks and Queues

 
[Page 605]

Chapter 18. Binary I/O

Mayan God Shel, Mexico. Photographer: Philip Coblentz. Courtesy Brand X Pictures.

Objectives

  • To understand how I/O is processed in Java ( §18.2).

  • To distinguish between text I/O and binary I/O ( §18.3).

  • To read and write bytes using FileInputStream and FileOutputStream ( §18.4.1).

  • To read and write primitive values and strings using DataInputStream / DataOutputStream ( §18.4.3).

  • To store and restore objects using ObjectOutputStream and ObjectInputStream , and to understand how objects are serialized and what kind of objects can be serialized ( §18.6).

  • To use the Serializable interface to enable objects to be serializable ( §18.6.1).

  • To know how to serialize arrays ( §18.6.2).

  • (Optional) To use RandomAccessFile for both read and write ( §18.7).


[Page 606]

18.1. Introduction

Data stored in a text file is represented in human-readable form. Data stored in a binary file is represented in binary form. You cannot read binary files. They are designed to be read by programs. For example, Java source programs are stored in text files and can be read by a text editor, but Java classes are stored in binary files and are read by the JVM. The advantage of binary files is that they are more efficient to process than text files.

Although it is not technically precise and correct, you can envision a text file as consisting of a sequence of characters and a binary file as consisting of a sequence of bits. For example, the decimal integer 199 is stored as the sequence of three characters, '1' , '9' , '9' , in a text file, and the same integer is stored as a byte -type value C7 in a binary file, because decimal 199 equals hex C7 (199 = 12 x 16 1 + 7).

Java offers many classes for performing file input and output. These classes can be categorized as text I/O classes and binary I/O classes . You learned how to read/write strings and numeric values from/to a text file using Scanner and PrintWriter in §8.8, "Text I/O." This section introduces the classes for performing binary I/O.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net