Chapter 18 Binary I/O
1. Although it is not technically precise, a text file consists of a sequence of
characters and a binary file consists of a sequence of bits. You can use a text
editor to view a text file, but not a binary file.
2. You have to use Java I/O classes to create objects and use the methods in the
objects to perform I/O. A Java I/O object is called a stream. An object for reading
data is called an input stream and an object for writing data is called an output
stream.
3. Binary I/O reads a byte from a file and copies it directly to the memory with any
conversion, vice versa. Text I/O requires encoding and decoding. The JVM
converts a Unicode to a file specific encoding when writing a character and
coverts a file specific encoding to a Unicode when reading a character.
4. Characters are represented using Unicode in the memory and characters are
represented in a file using a specified encoding scheme. If no encoding scheme is
specified, the system ’s default encoding scheme is used.
5. The values stored in the text file are 0x41 0x42 0x43.
6. If you write string "100" to an ASCII text file, the values stored are 0x31 0x30
0x30. If you write a numeric byte-type value 100 using binary I/O, the value
stored in the file is 0x64.
7. The encoding scheme for representing a character in a Java program is the
Unicode. By default, a text file is encoded using ASCII.
8. Almost all the methods and constructors in Java I/O classes, because there are
always some unexpected situation may arise during I/O.
9. Two reasons: (1) closing a stream ensures that data will be written to the file. (2)
closing a stream release resource acquired by the stream object.
10. The value of a byte is returned as an int in the range 0 to 255. If no byte is
available because the end of the stream has been reached, the value -1 is returned.
The only abstract method in InputStream is read() and the only abstract method in
OutputStream is write(int).
11. All the methods in FileInputStream/FileOutputStream are inherited from
InputStream/OutputStream. Use new FileInputStream(filename) or new
FileInputStream(File) to create a new FileInputStream and use new
FileOutputStream(filename), new FileOutputStream(File), new
评论0
最新资源