20.5. Heaps

 
[Page 606 ( continued )]

18.3. Text I/O vs. Binary I/O

Computers do not differentiate binary files and text files. All files are stored in binary format, and thus all files are essentially binary files. Text I/O is built upon binary I/O to provide a level of abstraction for character encoding and decoding, as shown in Figure 18.2(a). Encoding and decoding are automatically performed for text I/O. The JVM converts a Unicode to a file-specific encoding when writing a character and converts a file-specific encoding to a Unicode when reading a character. For example, suppose you write string "199" using text I/O to a file. Each character is written to the file. Since the Unicode for character ' 1 ' is 0x0031 , the Unicode 0x0031 is converted to a code that depends on the encoding scheme for the file. (Note that the prefix 0x denotes a hex number.) In the United States, the default encoding for text files on Windows is ASCII. The ASCII code for character ' 1 ' is 49 ( 0x31 in hex) and for character ' 9 ' is 57 ( 0x39 in hex). So to write the characters " 199 ", three bytes, 0x31 , 0x39 , and 0x39 , are sent to the output, as shown in Figure 18.2(a).


[Page 607]
Figure 18.2. Text I/O requires encoding and decoding, whereas binary I/O does not.

Note

JDK 1.5 supports supplementary Unicode . For simplicity, however, this book considers only the original Unicode from to FFFF .


Binary I/O does not require conversions. If you write a numeric value to a file using binary I/O, the exact value in the memory is copied into the file. For example, a byte-type value 199 is represented as 0xC7 (199 = 12 x 16 1 + 7) in the memory and appears exactly as 0xC7 in the file, as shown in Figure 18.2(b). When you read a byte using binary I/O, one byte value is read from the input.


[Page 608]

In general, you should use text input to read a file created by a text editor or a text output program, and use binary input to read a file created by a Java binary output program.

Binary I/O is more efficient than text I/O, because binary I/O does not require encoding and decoding. Binary files are independent of the encoding scheme on the host machine and thus are portable. Java programs on any machine can read a binary file created by a Java program. This is why Java class files are binary files. Java class files can run on a JVM on any machine.

Note

For consistency, this book uses the extension .txt to name text files and .dat to name binary files.


 


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